Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@uspk-ui/media-query

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@uspk-ui/media-query - npm Package Compare versions

Comparing version 2.0.0 to 2.0.1

292

dist/index.cjs.js

@@ -44,111 +44,86 @@ "use strict";

// src/media-query.tsx
var import_react_utils2 = require("@uspk-ui/react-utils");
var import_system = require("@uspk-ui/system");
var getBreakpoint = (theme, value) => {
var _a;
return ((_a = theme == null ? void 0 : theme.breakpoints) == null ? void 0 : _a[value]) ?? value;
};
function useQuery(props) {
const { breakpoint = "", below, above } = props;
const theme = (0, import_system.useTheme)();
const bpBelow = getBreakpoint(theme, below);
const bpAbove = getBreakpoint(theme, above);
let query = breakpoint;
if (bpBelow) {
query = `(max-width: ${bpBelow})`;
} else if (bpAbove) {
query = `(min-width: ${bpAbove})`;
}
return query;
}
// src/use-media-query.ts
var import_react_env = require("@uspk-ui/react-env");
var import_react_utils = require("@uspk-ui/react-utils");
var React2 = __toESM(require("react"));
var useSafeLayoutEffect = import_react_utils.isBrowser ? React2.useLayoutEffect : React2.useEffect;
function useMediaQuery(query) {
var import_react2 = require("react");
function useMediaQuery(query, options = {}) {
const { ssr = true, fallback } = options;
const env = (0, import_react_env.useEnvironment)();
const queries = Array.isArray(query) ? query : [query];
const isSupported = import_react_utils.isBrowser && "matchMedia" in env.window;
const [matches, setMatches] = React2.useState(
queries.map(
(query2) => isSupported ? !!env.window.matchMedia(query2).matches : false
)
);
useSafeLayoutEffect(() => {
if (!isSupported)
return void 0;
const mediaQueryList = queries.map((query2) => env.window.matchMedia(query2));
const listenerList = mediaQueryList.map((_, index) => {
const listener = (mqlEvent) => {
const queryIndex = mediaQueryList.findIndex(
(mediaQuery) => mediaQuery.media === mqlEvent.media
);
setMatches((matches2) => {
const currentMatches = matches2.map((x) => x);
currentMatches[queryIndex] = mqlEvent.matches;
return currentMatches;
let fallbackValues = Array.isArray(fallback) ? fallback : [fallback];
fallbackValues = fallbackValues.filter((v) => v != null);
const [value, setValue] = (0, import_react2.useState)(() => {
return queries.map((query2, index) => ({
media: query2,
matches: ssr ? !!fallbackValues[index] : env.window.matchMedia(query2).matches
}));
});
(0, import_react2.useEffect)(() => {
setValue(
queries.map((query2) => ({
media: query2,
matches: env.window.matchMedia(query2).matches
}))
);
const mql = queries.map((query2) => env.window.matchMedia(query2));
const handler = (evt) => {
setValue((prev) => {
return prev.slice().map((item) => {
if (item.media === evt.media)
return { ...item, matches: evt.matches };
return item;
});
};
if (typeof mediaQueryList[index].addEventListener === "function") {
mediaQueryList[index].addEventListener("change", listener);
});
};
mql.forEach((mql2) => {
if (typeof mql2.addListener === "function") {
mql2.addListener(handler);
} else {
mediaQueryList[index].addListener(listener);
mql2.addEventListener("change", handler);
}
return listener;
});
return () => {
mediaQueryList.forEach((_, index) => {
if (typeof mediaQueryList[index].removeEventListener === "function") {
mediaQueryList[index].removeEventListener(
"change",
listenerList[index]
);
mql.forEach((mql2) => {
if (typeof mql2.removeListener === "function") {
mql2.removeListener(handler);
} else {
mediaQueryList[index].removeListener(listenerList[index]);
mql2.removeEventListener("change", handler);
}
});
};
}, []);
return matches;
}, [env.window]);
return value.map((item) => item.matches);
}
// src/media-query.tsx
var import_jsx_runtime = require("react/jsx-runtime");
var Visibility = (props) => {
const { breakpoint, hide, children } = props;
const [show] = useMediaQuery(breakpoint);
const isVisible = hide ? !show : show;
const rendered = isVisible ? children : null;
return rendered;
};
var Hide = (props) => {
const { children } = props;
const query = useQuery(props);
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Visibility, {
breakpoint: query,
hide: true,
children
});
};
Hide.displayName = "Hide";
var Show = (props) => {
const { children } = props;
const query = useQuery(props);
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Visibility, {
breakpoint: query,
children
});
};
Show.displayName = "Show";
var getBreakpoint = (theme, value) => (0, import_react_utils2.memoizedGet)(theme, `breakpoints.${value}`, value);
function useQuery(props) {
const { breakpoint = "", below, above } = props;
const theme = (0, import_react_utils2.useTheme)();
const bpBelow = getBreakpoint(theme, below);
const bpAbove = getBreakpoint(theme, above);
let query = breakpoint;
if (bpBelow) {
query = `(max-width: ${bpBelow})`;
} else if (bpAbove) {
query = `(min-width: ${bpAbove})`;
}
return query;
}
// src/media-query.hook.tsx
function usePrefersReducedMotion() {
// src/media-query.hook.ts
function usePrefersReducedMotion(options) {
const [prefersReducedMotion] = useMediaQuery(
"(prefers-reduced-motion: reduce)"
"(prefers-reduced-motion: reduce)",
options
);
return prefersReducedMotion;
}
function useColorModePreference() {
const [isLight, isDark] = useMediaQuery([
"(prefers-color-scheme: light)",
"(prefers-color-scheme: dark)"
]);
function useColorModePreference(options) {
const [isLight, isDark] = useMediaQuery(
["(prefers-color-scheme: light)", "(prefers-color-scheme: dark)"],
options
);
if (isLight)

@@ -162,71 +137,37 @@ return "light";

// src/use-breakpoint.ts
var import_react2 = __toESM(require("react"));
var import_react_env2 = require("@uspk-ui/react-env");
var import_react_utils3 = require("@uspk-ui/react-utils");
function useBreakpoint(defaultBreakpoint = "base") {
const { __breakpoints } = (0, import_react_utils3.useTheme)();
const env = (0, import_react_env2.useEnvironment)();
const queries = import_react2.default.useMemo(
() => (__breakpoints == null ? void 0 : __breakpoints.details.map(({ minMaxQuery, breakpoint }) => ({
var import_system2 = require("@uspk-ui/system");
// ../../utilities/shared-utils/src/index.ts
function isObject(value) {
const type = typeof value;
return value != null && (type === "object" || type === "function") && !Array.isArray(value);
}
// src/use-breakpoint.ts
function useBreakpoint(arg) {
var _a;
const opts = isObject(arg) ? arg : { fallback: arg ?? "base" };
const theme = (0, import_system2.useTheme)();
const breakpoints = theme.__breakpoints.details.map(
({ minMaxQuery, breakpoint }) => ({
breakpoint,
query: minMaxQuery.replace("@media screen and ", "")
}))) ?? [],
[__breakpoints]
})
);
const [currentBreakpoint, setCurrentBreakpoint] = import_react2.default.useState(() => {
if (defaultBreakpoint) {
const fallbackBreakpointDetail = queries.find(
({ breakpoint }) => breakpoint === defaultBreakpoint
);
if (fallbackBreakpointDetail) {
return fallbackBreakpointDetail.breakpoint;
}
}
if (env.window.matchMedia) {
const matchingBreakpointDetail = queries.find(
({ query }) => env.window.matchMedia(query).matches
);
if (matchingBreakpointDetail) {
return matchingBreakpointDetail.breakpoint;
}
}
return void 0;
});
import_react2.default.useEffect(() => {
const allUnregisterFns = queries.map(({ breakpoint, query }) => {
const mediaQueryList = env.window.matchMedia(query);
if (mediaQueryList.matches) {
setCurrentBreakpoint(breakpoint);
}
const handleChange = (ev) => {
if (ev.matches) {
setCurrentBreakpoint(breakpoint);
}
};
if (typeof mediaQueryList.addEventListener === "function") {
mediaQueryList.addEventListener("change", handleChange);
} else {
mediaQueryList.addListener(handleChange);
}
return () => {
if (typeof mediaQueryList.removeEventListener === "function") {
mediaQueryList.removeEventListener("change", handleChange);
} else {
mediaQueryList.removeListener(handleChange);
}
};
});
return () => {
allUnregisterFns.forEach((unregister) => unregister());
};
}, [queries, __breakpoints, env.window]);
return currentBreakpoint;
const fallback = breakpoints.map((bp) => bp.breakpoint === opts.fallback);
const values = useMediaQuery(
breakpoints.map((bp) => bp.query),
{ fallback, ssr: opts.ssr }
);
const index = values.findIndex((value) => value == true);
return ((_a = breakpoints[index]) == null ? void 0 : _a.breakpoint) ?? opts.fallback;
}
// src/use-breakpoint-value.ts
var import_react_utils5 = require("@uspk-ui/react-utils");
var import_system3 = require("@uspk-ui/system");
var import_breakpoint_utils2 = require("@uspk-ui/breakpoint-utils");
// src/media-query.utils.ts
var import_react_utils4 = require("@uspk-ui/react-utils");
function getClosestValue(values, breakpoint, breakpoints = import_react_utils4.breakpoints) {
var import_breakpoint_utils = require("@uspk-ui/breakpoint-utils");
function getClosestValue(values, breakpoint, breakpoints = import_breakpoint_utils.breakpoints) {
let index = Object.keys(values).indexOf(breakpoint);

@@ -239,3 +180,3 @@ if (index !== -1) {

const key = breakpoints[stopIndex];
if (values[key] != null) {
if (values.hasOwnProperty(key)) {
index = stopIndex;

@@ -254,11 +195,12 @@ break;

// src/use-breakpoint-value.ts
function useBreakpointValue(values, defaultBreakpoint) {
function useBreakpointValue(values, arg) {
var _a;
const breakpoint = useBreakpoint(defaultBreakpoint);
const theme = (0, import_react_utils5.useTheme)();
const opts = isObject(arg) ? arg : { fallback: arg ?? "base" };
const breakpoint = useBreakpoint(opts);
const theme = (0, import_system3.useTheme)();
if (!breakpoint)
return void 0;
return;
const breakpoints = Array.from(((_a = theme.__breakpoints) == null ? void 0 : _a.keys) || []);
const obj = (0, import_react_utils5.isArray)(values) ? (0, import_react_utils5.fromEntries)(
Object.entries((0, import_react_utils5.arrayToObjectNotation)(values, breakpoints)).map(
const obj = Array.isArray(values) ? Object.fromEntries(
Object.entries((0, import_breakpoint_utils2.arrayToObjectNotation)(values, breakpoints)).map(
([key, value]) => [key, value]

@@ -269,2 +211,38 @@ )

}
// src/visibility.tsx
function Visibility(props) {
const { breakpoint, hide, children, ssr } = props;
const [show] = useMediaQuery(breakpoint, { ssr });
const isVisible = hide ? !show : show;
const rendered = isVisible ? children : null;
return rendered;
}
// src/show.tsx
var import_jsx_runtime = require("react/jsx-runtime");
function Show(props) {
const { children, ssr } = props;
const query = useQuery(props);
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Visibility, {
breakpoint: query,
ssr,
children
});
}
Show.displayName = "Show";
// src/hide.tsx
var import_jsx_runtime = require("react/jsx-runtime");
function Hide(props) {
const { children, ssr } = props;
const query = useQuery(props);
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Visibility, {
breakpoint: query,
hide: true,
ssr,
children
});
}
Hide.displayName = "Hide";
// Annotate the CommonJS export names for ESM import in node:

@@ -271,0 +249,0 @@ 0 && (module.exports = {

@@ -1,24 +0,1 @@

import * as React from 'react';
declare type HideProps = ShowProps;
declare const Hide: React.FC<HideProps>;
interface ShowProps {
/**
* A custom css media query that determines when the `children` are rendered.
* Will render `children` if that query resolves to `true`.
*/
breakpoint?: string;
/**
* A value from the `breakpoints` section in the theme. Will render `children`
* from that breakpoint and below. Default breakpoint values: `sm`, `md`, `lg`, `xl`, `2xl`.
*/
below?: string;
/**
* A value from the `breakpoints` section in the theme. Will render `children`
* from that breakpoint and above. Default breakpoint values: `sm`, `md`, `lg`, `xl`, `2xl`.
*/
above?: string;
children?: React.ReactNode;
}
declare const Show: React.FC<ShowProps>;
interface UseQueryProps {

@@ -31,37 +8,41 @@ breakpoint?: string;

declare type UseMediaQueryOptions = {
fallback?: boolean | boolean[];
ssr?: boolean;
};
/**
* React hook that tracks state of a CSS media query
*
* @param query the media query to match
* @param options the media query options { fallback, ssr }
*
*/
declare function useMediaQuery(query: string | string[], options?: UseMediaQueryOptions): boolean[];
/**
* React hook used to get the user's animation preference.
*
*/
declare function usePrefersReducedMotion(): boolean;
declare function usePrefersReducedMotion(options?: UseMediaQueryOptions): boolean;
/**
* React hook for getting the user's color mode preference.
*/
declare function useColorModePreference(): "dark" | "light" | undefined;
declare function useColorModePreference(options?: UseMediaQueryOptions): "dark" | "light" | undefined;
declare type UseBreakpointOptions = {
ssr?: boolean;
fallback?: string;
};
/**
* React hook used to get the current responsive media breakpoint.
*
* @param [defaultBreakpoint="base"] default breakpoint name
* (in non-window environments like SSR)
*
* For SSR, you can use a package like [is-mobile](https://github.com/kaimallea/isMobile)
* to get the default breakpoint value from the user-agent
* to get the default breakpoint value from the user-agent.
*/
declare function useBreakpoint(defaultBreakpoint?: string): any;
declare function useBreakpoint(arg?: string | UseBreakpointOptions): string;
/**
* React hook that tracks state of a CSS media query
*
* @param query the media query to match
*/
declare function useMediaQuery(query: string | string[]): boolean[];
/**
* React hook for getting the value for the current breakpoint from the
* provided responsive values object.
*
* @param values
* @param [defaultBreakpoint] default breakpoint name
* (in non-window environments like SSR)
*
* For SSR, you can use a package like [is-mobile](https://github.com/kaimallea/isMobile)

@@ -72,5 +53,44 @@ * to get the default breakpoint value from the user-agent

* const width = useBreakpointValue({ base: '150px', md: '250px' })
*
*/
declare function useBreakpointValue<T = any>(values: Partial<Record<string, T>> | T[], defaultBreakpoint?: string): T | undefined;
declare function useBreakpointValue<T = any>(values: Partial<Record<string, T>> | T[], arg?: UseBreakpointOptions | string): T | undefined;
export { Hide, HideProps, Show, ShowProps, UseQueryProps, useBreakpoint, useBreakpointValue, useColorModePreference, useMediaQuery, usePrefersReducedMotion, useQuery };
interface ShowProps {
/**
* A custom css media query that determines when the `children` are rendered.
* Will render `children` if that query resolves to `true`.
*/
breakpoint?: string;
/**
* A value from the `breakpoints` section in the theme. Will render `children`
* from that breakpoint and below. Default breakpoint values: `sm`, `md`, `lg`, `xl`, `2xl`.
*/
below?: string;
/**
* A value from the `breakpoints` section in the theme. Will render `children`
* from that breakpoint and above. Default breakpoint values: `sm`, `md`, `lg`, `xl`, `2xl`.
*/
above?: string;
ssr?: boolean;
children?: React.ReactNode;
}
/**
* `Show` wraps a component to render if the provided media query matches.
*
*/
declare function Show(props: ShowProps): JSX.Element;
declare namespace Show {
var displayName: string;
}
declare type HideProps = ShowProps;
/**
* `Hide` wraps a component to not render if the provided media query matches.
*
*/
declare function Hide(props: HideProps): JSX.Element;
declare namespace Hide {
var displayName: string;
}
export { Hide, HideProps, Show, ShowProps, UseBreakpointOptions, UseMediaQueryOptions, UseQueryProps, useBreakpoint, useBreakpointValue, useColorModePreference, useMediaQuery, usePrefersReducedMotion, useQuery };

@@ -5,111 +5,86 @@ // ../../../react-shim.js

// src/media-query.tsx
import { memoizedGet as get, useTheme } from "@uspk-ui/react-utils";
import { useTheme } from "@uspk-ui/system";
var getBreakpoint = (theme, value) => {
var _a;
return ((_a = theme == null ? void 0 : theme.breakpoints) == null ? void 0 : _a[value]) ?? value;
};
function useQuery(props) {
const { breakpoint = "", below, above } = props;
const theme = useTheme();
const bpBelow = getBreakpoint(theme, below);
const bpAbove = getBreakpoint(theme, above);
let query = breakpoint;
if (bpBelow) {
query = `(max-width: ${bpBelow})`;
} else if (bpAbove) {
query = `(min-width: ${bpAbove})`;
}
return query;
}
// src/use-media-query.ts
import { useEnvironment } from "@uspk-ui/react-env";
import { isBrowser } from "@uspk-ui/react-utils";
import * as React2 from "react";
var useSafeLayoutEffect = isBrowser ? React2.useLayoutEffect : React2.useEffect;
function useMediaQuery(query) {
import { useEffect, useState } from "react";
function useMediaQuery(query, options = {}) {
const { ssr = true, fallback } = options;
const env = useEnvironment();
const queries = Array.isArray(query) ? query : [query];
const isSupported = isBrowser && "matchMedia" in env.window;
const [matches, setMatches] = React2.useState(
queries.map(
(query2) => isSupported ? !!env.window.matchMedia(query2).matches : false
)
);
useSafeLayoutEffect(() => {
if (!isSupported)
return void 0;
const mediaQueryList = queries.map((query2) => env.window.matchMedia(query2));
const listenerList = mediaQueryList.map((_, index) => {
const listener = (mqlEvent) => {
const queryIndex = mediaQueryList.findIndex(
(mediaQuery) => mediaQuery.media === mqlEvent.media
);
setMatches((matches2) => {
const currentMatches = matches2.map((x) => x);
currentMatches[queryIndex] = mqlEvent.matches;
return currentMatches;
let fallbackValues = Array.isArray(fallback) ? fallback : [fallback];
fallbackValues = fallbackValues.filter((v) => v != null);
const [value, setValue] = useState(() => {
return queries.map((query2, index) => ({
media: query2,
matches: ssr ? !!fallbackValues[index] : env.window.matchMedia(query2).matches
}));
});
useEffect(() => {
setValue(
queries.map((query2) => ({
media: query2,
matches: env.window.matchMedia(query2).matches
}))
);
const mql = queries.map((query2) => env.window.matchMedia(query2));
const handler = (evt) => {
setValue((prev) => {
return prev.slice().map((item) => {
if (item.media === evt.media)
return { ...item, matches: evt.matches };
return item;
});
};
if (typeof mediaQueryList[index].addEventListener === "function") {
mediaQueryList[index].addEventListener("change", listener);
});
};
mql.forEach((mql2) => {
if (typeof mql2.addListener === "function") {
mql2.addListener(handler);
} else {
mediaQueryList[index].addListener(listener);
mql2.addEventListener("change", handler);
}
return listener;
});
return () => {
mediaQueryList.forEach((_, index) => {
if (typeof mediaQueryList[index].removeEventListener === "function") {
mediaQueryList[index].removeEventListener(
"change",
listenerList[index]
);
mql.forEach((mql2) => {
if (typeof mql2.removeListener === "function") {
mql2.removeListener(handler);
} else {
mediaQueryList[index].removeListener(listenerList[index]);
mql2.removeEventListener("change", handler);
}
});
};
}, []);
return matches;
}, [env.window]);
return value.map((item) => item.matches);
}
// src/media-query.tsx
import { jsx } from "react/jsx-runtime";
var Visibility = (props) => {
const { breakpoint, hide, children } = props;
const [show] = useMediaQuery(breakpoint);
const isVisible = hide ? !show : show;
const rendered = isVisible ? children : null;
return rendered;
};
var Hide = (props) => {
const { children } = props;
const query = useQuery(props);
return /* @__PURE__ */ jsx(Visibility, {
breakpoint: query,
hide: true,
children
});
};
Hide.displayName = "Hide";
var Show = (props) => {
const { children } = props;
const query = useQuery(props);
return /* @__PURE__ */ jsx(Visibility, {
breakpoint: query,
children
});
};
Show.displayName = "Show";
var getBreakpoint = (theme, value) => get(theme, `breakpoints.${value}`, value);
function useQuery(props) {
const { breakpoint = "", below, above } = props;
const theme = useTheme();
const bpBelow = getBreakpoint(theme, below);
const bpAbove = getBreakpoint(theme, above);
let query = breakpoint;
if (bpBelow) {
query = `(max-width: ${bpBelow})`;
} else if (bpAbove) {
query = `(min-width: ${bpAbove})`;
}
return query;
}
// src/media-query.hook.tsx
function usePrefersReducedMotion() {
// src/media-query.hook.ts
function usePrefersReducedMotion(options) {
const [prefersReducedMotion] = useMediaQuery(
"(prefers-reduced-motion: reduce)"
"(prefers-reduced-motion: reduce)",
options
);
return prefersReducedMotion;
}
function useColorModePreference() {
const [isLight, isDark] = useMediaQuery([
"(prefers-color-scheme: light)",
"(prefers-color-scheme: dark)"
]);
function useColorModePreference(options) {
const [isLight, isDark] = useMediaQuery(
["(prefers-color-scheme: light)", "(prefers-color-scheme: dark)"],
options
);
if (isLight)

@@ -123,75 +98,36 @@ return "light";

// src/use-breakpoint.ts
import React3 from "react";
import { useEnvironment as useEnvironment2 } from "@uspk-ui/react-env";
import { useTheme as useTheme2 } from "@uspk-ui/react-utils";
function useBreakpoint(defaultBreakpoint = "base") {
const { __breakpoints } = useTheme2();
const env = useEnvironment2();
const queries = React3.useMemo(
() => (__breakpoints == null ? void 0 : __breakpoints.details.map(({ minMaxQuery, breakpoint }) => ({
import { useTheme as useTheme2 } from "@uspk-ui/system";
// ../../utilities/shared-utils/src/index.ts
function isObject(value) {
const type = typeof value;
return value != null && (type === "object" || type === "function") && !Array.isArray(value);
}
// src/use-breakpoint.ts
function useBreakpoint(arg) {
var _a;
const opts = isObject(arg) ? arg : { fallback: arg ?? "base" };
const theme = useTheme2();
const breakpoints = theme.__breakpoints.details.map(
({ minMaxQuery, breakpoint }) => ({
breakpoint,
query: minMaxQuery.replace("@media screen and ", "")
}))) ?? [],
[__breakpoints]
})
);
const [currentBreakpoint, setCurrentBreakpoint] = React3.useState(() => {
if (defaultBreakpoint) {
const fallbackBreakpointDetail = queries.find(
({ breakpoint }) => breakpoint === defaultBreakpoint
);
if (fallbackBreakpointDetail) {
return fallbackBreakpointDetail.breakpoint;
}
}
if (env.window.matchMedia) {
const matchingBreakpointDetail = queries.find(
({ query }) => env.window.matchMedia(query).matches
);
if (matchingBreakpointDetail) {
return matchingBreakpointDetail.breakpoint;
}
}
return void 0;
});
React3.useEffect(() => {
const allUnregisterFns = queries.map(({ breakpoint, query }) => {
const mediaQueryList = env.window.matchMedia(query);
if (mediaQueryList.matches) {
setCurrentBreakpoint(breakpoint);
}
const handleChange = (ev) => {
if (ev.matches) {
setCurrentBreakpoint(breakpoint);
}
};
if (typeof mediaQueryList.addEventListener === "function") {
mediaQueryList.addEventListener("change", handleChange);
} else {
mediaQueryList.addListener(handleChange);
}
return () => {
if (typeof mediaQueryList.removeEventListener === "function") {
mediaQueryList.removeEventListener("change", handleChange);
} else {
mediaQueryList.removeListener(handleChange);
}
};
});
return () => {
allUnregisterFns.forEach((unregister) => unregister());
};
}, [queries, __breakpoints, env.window]);
return currentBreakpoint;
const fallback = breakpoints.map((bp) => bp.breakpoint === opts.fallback);
const values = useMediaQuery(
breakpoints.map((bp) => bp.query),
{ fallback, ssr: opts.ssr }
);
const index = values.findIndex((value) => value == true);
return ((_a = breakpoints[index]) == null ? void 0 : _a.breakpoint) ?? opts.fallback;
}
// src/use-breakpoint-value.ts
import {
arrayToObjectNotation,
fromEntries,
isArray,
useTheme as useTheme3
} from "@uspk-ui/react-utils";
import { useTheme as useTheme3 } from "@uspk-ui/system";
import { arrayToObjectNotation } from "@uspk-ui/breakpoint-utils";
// src/media-query.utils.ts
import { breakpoints as defaultBreakPoints } from "@uspk-ui/react-utils";
import { breakpoints as defaultBreakPoints } from "@uspk-ui/breakpoint-utils";
function getClosestValue(values, breakpoint, breakpoints = defaultBreakPoints) {

@@ -205,3 +141,3 @@ let index = Object.keys(values).indexOf(breakpoint);

const key = breakpoints[stopIndex];
if (values[key] != null) {
if (values.hasOwnProperty(key)) {
index = stopIndex;

@@ -220,10 +156,11 @@ break;

// src/use-breakpoint-value.ts
function useBreakpointValue(values, defaultBreakpoint) {
function useBreakpointValue(values, arg) {
var _a;
const breakpoint = useBreakpoint(defaultBreakpoint);
const opts = isObject(arg) ? arg : { fallback: arg ?? "base" };
const breakpoint = useBreakpoint(opts);
const theme = useTheme3();
if (!breakpoint)
return void 0;
return;
const breakpoints = Array.from(((_a = theme.__breakpoints) == null ? void 0 : _a.keys) || []);
const obj = isArray(values) ? fromEntries(
const obj = Array.isArray(values) ? Object.fromEntries(
Object.entries(arrayToObjectNotation(values, breakpoints)).map(

@@ -235,2 +172,38 @@ ([key, value]) => [key, value]

}
// src/visibility.tsx
function Visibility(props) {
const { breakpoint, hide, children, ssr } = props;
const [show] = useMediaQuery(breakpoint, { ssr });
const isVisible = hide ? !show : show;
const rendered = isVisible ? children : null;
return rendered;
}
// src/show.tsx
import { jsx } from "react/jsx-runtime";
function Show(props) {
const { children, ssr } = props;
const query = useQuery(props);
return /* @__PURE__ */ jsx(Visibility, {
breakpoint: query,
ssr,
children
});
}
Show.displayName = "Show";
// src/hide.tsx
import { jsx as jsx2 } from "react/jsx-runtime";
function Hide(props) {
const { children, ssr } = props;
const query = useQuery(props);
return /* @__PURE__ */ jsx2(Visibility, {
breakpoint: query,
hide: true,
ssr,
children
});
}
Hide.displayName = "Hide";
export {

@@ -237,0 +210,0 @@ Hide,

{
"name": "@uspk-ui/media-query",
"version": "2.0.0",
"version": "2.0.1",
"description": "React hook to change visibility of a component based on css media query",

@@ -11,11 +11,7 @@ "keywords": [

"license": "MIT",
"sideEffects": false,
"main": "dist/index.cjs.js",
"module": "dist/index.esm.js",
"types": "dist/index.d.ts",
"sideEffects": false,
"files": [
"dist",
"src"
"dist"
],
"preconstruct": {},
"publishConfig": {

@@ -33,7 +29,9 @@ "access": "public"

"dependencies": {
"@uspk-ui/react-env": "2.0.0",
"@uspk-ui/react-utils": "2.0.0"
"@uspk-ui/react-env": "2.1.0",
"@uspk-ui/breakpoint-utils": "1.0.0"
},
"devDependencies": {
"@uspk-ui/theme": "*",
"@uspk-ui/shared-utils": "1.0.0",
"@uspk-ui/system": "1.0.0",
"@uspk-ui/theme": "2.1.0",
"@types/react-frame-component": "^4.1.1",

@@ -44,11 +42,10 @@ "jest-matchmedia-mock": "^1.1.0",

"react": "^18.0.0",
"jest": "13.0.0",
"prop-types": "^15.5.9",
"react-dom": "^18.0.0",
"clean-package": "2.1.1"
},
"peerDependencies": {
"@uspk-ui/theme": ">=1.0.0",
"@uspk-ui/system": ">=1.0.0",
"react": ">=18"
},
"module": "dist/index.esm.js",
"types": "dist/index.d.ts",
"exports": {

@@ -55,0 +52,0 @@ ".": {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc