Skip to content

Commit ea4ca67

Browse files
authored
Merge pull request #443 from ownego/update/optimize-breakpoints
2 parents fc5aac5 + ed8ee77 commit ea4ca67

File tree

4 files changed

+44
-21
lines changed

4 files changed

+44
-21
lines changed

src/components/AppProvider/AppProvider.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { getTheme } from '@/utilities/use-theme';
2222
import { useMediaQueryContext } from '@/use/useMediaQuery';
2323
import { useFocusManagerContext } from '@/use/useFocusManager';
2424
import { useEphemeralPresenceManagerContext } from '@/use/useEphemeralPresenceManager';
25+
import { useBreakpointsContext } from '@/use/useBreakpoints';
2526
2627
export type AppProviderProps = {
2728
theme?: ThemeName;
@@ -52,6 +53,7 @@ const scrollLockManager = new ScrollLockManager();
5253
const { isNavigationCollapsed } = useMediaQueryContext();
5354
const focusManager = useFocusManagerContext();
5455
const ephemeralPresenceManager = useEphemeralPresenceManagerContext();
56+
const { breakpoints } = useBreakpointsContext();
5557
5658
const portalsContainerRef = ref<HTMLElement | null>(null);
5759
@@ -152,6 +154,7 @@ provide('media-query', { isNavigationCollapsed });
152154
provide('portals-manager', portalsContainerRef);
153155
provide('focus-manager', focusManager);
154156
provide('ephemeral-presence-manager', ephemeralPresenceManager);
157+
provide('breakpoints', breakpoints);
155158
</script>
156159

157160
<style lang="scss">

src/use/useBreakpoints.ts

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1-
import { onBeforeUnmount, ref } from 'vue';
1+
import { onBeforeUnmount, ref, inject } from 'vue';
22
import {getMediaConditions, themeDefault} from '@shopify/polaris-tokens';
3+
import type { BreakpointsTokenGroup } from '@shopify/polaris-tokens';
34
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+
}
814

915
const Breakpoints = {
1016
// TODO: Update to smDown
@@ -38,20 +44,6 @@ export function stackedContent() {
3844
: window.matchMedia(`(max-width: ${Breakpoints.stackedContent})`);
3945
}
4046

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-
};
5547

5648
const breakpointsQueryEntries = getBreakpointsQueryEntries(
5749
themeDefault.breakpoints,
@@ -122,7 +114,7 @@ export interface UseBreakpointsOptions {
122114
* const breakpoints = useBreakpoints({defaults: true});
123115
* breakpoints //=> All values will be `true` during SSR
124116
*/
125-
export function useBreakpoints(options?: UseBreakpointsOptions) {
117+
export function useBreakpointsContext(options?: UseBreakpointsOptions) {
126118
// On SSR, and initial CSR, we force usage of the defaults to avoid a
127119
// hydration mismatch error.
128120
// Later, in the effect, we will call this again on the client side without
@@ -163,7 +155,9 @@ export function useBreakpoints(options?: UseBreakpointsOptions) {
163155
});
164156
});
165157

166-
return breakpoints;
158+
return {
159+
breakpoints,
160+
}
167161
}
168162

169163
/**
@@ -200,3 +194,4 @@ export function getBreakpointsQueryEntries(breakpoints: BreakpointsTokenGroup) {
200194
function capitalize(str: string) {
201195
return str.charAt(0).toUpperCase() + str.slice(1);
202196
}
197+

src/utilities/breakpoints/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './types';

src/utilities/breakpoints/types.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import type { Ref } from 'vue';
2+
import type {
3+
BreakpointsAlias,
4+
BreakpointsAliasDirection,
5+
} from '@shopify/polaris-tokens';
6+
7+
/**
8+
* Directional alias for each Polaris `breakpoints` token.
9+
*
10+
* @example 'smUp' | 'smDown' | 'smOnly' | 'mdUp' | etc.
11+
*/
12+
export type BreakpointsDirectionAlias =
13+
`${BreakpointsAlias}${Capitalize<BreakpointsAliasDirection>}`;
14+
15+
/**
16+
* Match results for each directional Polaris `breakpoints` alias.
17+
*/
18+
export type BreakpointsMatches = {
19+
[DirectionAlias in BreakpointsDirectionAlias]: boolean;
20+
};
21+
22+
export interface BreakpointsContextType {
23+
breakpoints: Ref<BreakpointsMatches>;
24+
}

0 commit comments

Comments
 (0)