|
1 | | -import { onBeforeUnmount, ref } from 'vue'; |
| 1 | +import { onBeforeUnmount, ref, inject } from 'vue'; |
2 | 2 | import {getMediaConditions, themeDefault} from '@shopify/polaris-tokens'; |
| 3 | +import type { BreakpointsTokenGroup } from '@shopify/polaris-tokens'; |
3 | 4 | import type { |
4 | | - BreakpointsAlias, |
5 | | - BreakpointsAliasDirection, |
6 | | - BreakpointsTokenGroup, |
7 | | -} from '@shopify/polaris-tokens'; |
| 5 | + BreakpointsDirectionAlias, |
| 6 | + BreakpointsMatches, |
| 7 | + BreakpointsContextType |
| 8 | + } from '@/utilities/breakpoints'; |
| 9 | + |
| 10 | +export function useBreakpoints() { |
| 11 | + const context = inject('breakpoints', {}) as BreakpointsContextType; |
| 12 | + return context; |
| 13 | +} |
8 | 14 |
|
9 | 15 | const Breakpoints = { |
10 | 16 | // TODO: Update to smDown |
@@ -38,20 +44,6 @@ export function stackedContent() { |
38 | 44 | : window.matchMedia(`(max-width: ${Breakpoints.stackedContent})`); |
39 | 45 | } |
40 | 46 |
|
41 | | -/** |
42 | | - * Directional alias for each Polaris `breakpoints` token. |
43 | | - * |
44 | | - * @example 'smUp' | 'smDown' | 'smOnly' | 'mdUp' | etc. |
45 | | - */ |
46 | | -export type BreakpointsDirectionAlias = |
47 | | - `${BreakpointsAlias}${Capitalize<BreakpointsAliasDirection>}`; |
48 | | - |
49 | | -/** |
50 | | - * Match results for each directional Polaris `breakpoints` alias. |
51 | | - */ |
52 | | -type BreakpointsMatches = { |
53 | | - [DirectionAlias in BreakpointsDirectionAlias]: boolean; |
54 | | -}; |
55 | 47 |
|
56 | 48 | const breakpointsQueryEntries = getBreakpointsQueryEntries( |
57 | 49 | themeDefault.breakpoints, |
@@ -122,7 +114,7 @@ export interface UseBreakpointsOptions { |
122 | 114 | * const breakpoints = useBreakpoints({defaults: true}); |
123 | 115 | * breakpoints //=> All values will be `true` during SSR |
124 | 116 | */ |
125 | | -export function useBreakpoints(options?: UseBreakpointsOptions) { |
| 117 | +export function useBreakpointsContext(options?: UseBreakpointsOptions) { |
126 | 118 | // On SSR, and initial CSR, we force usage of the defaults to avoid a |
127 | 119 | // hydration mismatch error. |
128 | 120 | // Later, in the effect, we will call this again on the client side without |
@@ -163,7 +155,9 @@ export function useBreakpoints(options?: UseBreakpointsOptions) { |
163 | 155 | }); |
164 | 156 | }); |
165 | 157 |
|
166 | | - return breakpoints; |
| 158 | + return { |
| 159 | + breakpoints, |
| 160 | + } |
167 | 161 | } |
168 | 162 |
|
169 | 163 | /** |
@@ -200,3 +194,4 @@ export function getBreakpointsQueryEntries(breakpoints: BreakpointsTokenGroup) { |
200 | 194 | function capitalize(str: string) { |
201 | 195 | return str.charAt(0).toUpperCase() + str.slice(1); |
202 | 196 | } |
| 197 | + |
0 commit comments