|
| 1 | +import type { |
| 2 | + BreakpointsAlias, |
| 3 | + BreakpointsAliasDirection, |
| 4 | + BreakpointsTokenGroup, |
| 5 | +} from '@shopify/polaris-tokens'; |
| 6 | + |
| 7 | +import { |
| 8 | + getMediaConditions, |
| 9 | + tokens, |
| 10 | +} from '@shopify/polaris-tokens'; |
| 11 | + |
| 12 | +import {isServer} from 'polaris/polaris-react/src/utilities/target'; |
| 13 | + |
| 14 | +const Breakpoints = { |
| 15 | + navigationBarCollapsed: '768px', |
| 16 | + stackedContent: '1043px', |
| 17 | +}; |
| 18 | + |
| 19 | +const noWindowMatches: MediaQueryList = { |
| 20 | + media: '', |
| 21 | + addListener: noop, |
| 22 | + removeListener: noop, |
| 23 | + matches: false, |
| 24 | + onchange: noop, |
| 25 | + addEventListener: noop, |
| 26 | + removeEventListener: noop, |
| 27 | + dispatchEvent: (_: Event) => true, |
| 28 | +}; |
| 29 | + |
| 30 | +function noop() { return; } |
| 31 | + |
| 32 | +export function navigationBarCollapsed() { |
| 33 | + return typeof window === 'undefined' |
| 34 | + ? noWindowMatches |
| 35 | + : window.matchMedia(`(max-width: ${Breakpoints.navigationBarCollapsed})`); |
| 36 | +} |
| 37 | + |
| 38 | +export function stackedContent() { |
| 39 | + return typeof window === 'undefined' |
| 40 | + ? noWindowMatches |
| 41 | + : window.matchMedia(`(max-width: ${Breakpoints.stackedContent})`); |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Directional alias for each Polaris `breakpoints` token. |
| 46 | + * |
| 47 | + * @example 'smUp' | 'smDown' | 'smOnly' | 'mdUp' | etc. |
| 48 | + */ |
| 49 | +export type BreakpointsDirectionAlias = |
| 50 | + `${BreakpointsAlias}${Capitalize<BreakpointsAliasDirection>}`; |
| 51 | + |
| 52 | +/** |
| 53 | + * Match results for each directional Polaris `breakpoints` alias. |
| 54 | + */ |
| 55 | +type BreakpointsMatches = { |
| 56 | + [DirectionAlias in BreakpointsDirectionAlias]: boolean; |
| 57 | +}; |
| 58 | + |
| 59 | +const breakpointsQueryEntries = getBreakpointsQueryEntries(tokens.breakpoints); |
| 60 | + |
| 61 | +function getMatches(defaults?: UseBreakpointsOptions['defaults']) { |
| 62 | + if (!isServer) { |
| 63 | + return Object.fromEntries( |
| 64 | + breakpointsQueryEntries.map(([directionAlias, query]) => [ |
| 65 | + directionAlias, |
| 66 | + window.matchMedia(query).matches, |
| 67 | + ]), |
| 68 | + ) as BreakpointsMatches; |
| 69 | + } |
| 70 | + |
| 71 | + if (typeof defaults === 'object' && defaults !== null) { |
| 72 | + return Object.fromEntries( |
| 73 | + breakpointsQueryEntries.map(([directionAlias]) => [ |
| 74 | + directionAlias, |
| 75 | + defaults[directionAlias] ?? false, |
| 76 | + ]), |
| 77 | + ) as BreakpointsMatches; |
| 78 | + } |
| 79 | + |
| 80 | + return Object.fromEntries( |
| 81 | + breakpointsQueryEntries.map(([directionAlias]) => [ |
| 82 | + directionAlias, |
| 83 | + defaults ?? false, |
| 84 | + ]), |
| 85 | + ) as BreakpointsMatches; |
| 86 | +} |
| 87 | + |
| 88 | +export interface UseBreakpointsOptions { |
| 89 | + /** |
| 90 | + * Default values applied during SSR. Accepts a single value to use for each |
| 91 | + * breakpoint alias, or an object for configuring select breakpoints. |
| 92 | + * |
| 93 | + * @default false |
| 94 | + */ |
| 95 | + defaults: |
| 96 | + | boolean |
| 97 | + | { |
| 98 | + [DirectionAlias in BreakpointsDirectionAlias]?: boolean; |
| 99 | + }; |
| 100 | +} |
| 101 | + |
| 102 | +/** |
| 103 | + * Retrieves media query matches for each directional Polaris `breakpoints` alias. |
| 104 | + * |
| 105 | + * @example |
| 106 | + * const {smUp} = useBreakpoints(); |
| 107 | + * return smUp && 'Hello world'; |
| 108 | + * |
| 109 | + * @example |
| 110 | + * const {mdUp} = useBreakpoints({defaults: {mdUp: true}}); |
| 111 | + * mdUp //=> `true` during SSR |
| 112 | + * |
| 113 | + * @example |
| 114 | + * const breakpoints = useBreakpoints({defaults: true}); |
| 115 | + * breakpoints //=> All values will be `true` during SSR |
| 116 | + */ |
| 117 | +// export function useBreakpoints(options?: UseBreakpointsOptions) { |
| 118 | +// const [breakpoints, setBreakpoints] = useState(getMatches(options?.defaults)); |
| 119 | + |
| 120 | +// useIsomorphicLayoutEffect(() => { |
| 121 | +// const mediaQueryLists = breakpointsQueryEntries.map(([_, query]) => |
| 122 | +// window.matchMedia(query), |
| 123 | +// ); |
| 124 | + |
| 125 | +// const handler = () => setBreakpoints(getMatches()); |
| 126 | + |
| 127 | +// mediaQueryLists.forEach((mql) => { |
| 128 | +// mql.addEventListener('change', handler); |
| 129 | +// }); |
| 130 | + |
| 131 | +// return () => |
| 132 | +// mediaQueryLists.forEach((mql) => { |
| 133 | +// mql.removeEventListener('change', handler); |
| 134 | +// }); |
| 135 | +// }, []); |
| 136 | + |
| 137 | +// return breakpoints; |
| 138 | +// } |
| 139 | + |
| 140 | +/** |
| 141 | + * Converts `breakpoints` tokens into directional media query entries. |
| 142 | + * |
| 143 | + * @example |
| 144 | + * const breakpointsQueryEntries = getBreakpointsQueryEntries(breakpoints); |
| 145 | + * breakpointsQueryEntries === [ |
| 146 | + * ['xsUp', '(min-width: ...)'], |
| 147 | + * ['xsDown', '(max-width: ...)'], |
| 148 | + * ['xsOnly', '(min-width: ...) and (max-width: ...)'], |
| 149 | + * ['smUp', '(min-width: ...) and (max-width: ...)'], |
| 150 | + * ['mdUp', '(min-width: ...) and (max-width: ...)'], |
| 151 | + * // etc. |
| 152 | + * ] |
| 153 | + */ |
| 154 | +export function getBreakpointsQueryEntries(breakpoints: BreakpointsTokenGroup) { |
| 155 | + const mediaConditionEntries = Object.entries(getMediaConditions(breakpoints)); |
| 156 | + |
| 157 | + return mediaConditionEntries |
| 158 | + .map(([breakpointsToken, mediaConditions]) => |
| 159 | + Object.entries(mediaConditions).map(([direction, mediaCondition]) => { |
| 160 | + const breakpointsAlias = breakpointsToken.split('-')[1]; |
| 161 | + |
| 162 | + // e.g. smUp, smDown, smOnly, etc. |
| 163 | + const directionAlias = `${breakpointsAlias}${capitalize(direction)}`; |
| 164 | + |
| 165 | + return [directionAlias, mediaCondition]; |
| 166 | + }), |
| 167 | + ) |
| 168 | + .flat() as [BreakpointsDirectionAlias, string][]; |
| 169 | +} |
| 170 | + |
| 171 | +function capitalize(str: string) { |
| 172 | + return str.charAt(0).toUpperCase() + str.slice(1); |
| 173 | +} |
0 commit comments