-
Notifications
You must be signed in to change notification settings - Fork 227
/
Copy pathuseResponsiveContainerStyles.ts
128 lines (115 loc) · 3.69 KB
/
useResponsiveContainerStyles.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import { useTheme, breakpointKeys, CanvasBreakpoints } from "@workday/canvas-kit-react/common";
import type {AllStyleProps} from "@workday/canvas-kit-react/layout";
import {isWithinBreakpoint} from '../utils/isWithinBreakpoint'
export type BreakpointKeys = typeof breakpointKeys[number];
type ResponsiveCSSObject<T> = {
[P in keyof T]: Partial<Record<BreakpointKeys, AllStyleProps>> &
AllStyleProps;
};
type CSSObject<T> = {
[P in keyof T]: AllStyleProps;
};
// Takes in CanvasBreakpoint keys and returns current breakpoint key. Current breakpoint key is determined by the `width` of the container
const getSize = (width: number, breakpoints: CanvasBreakpoints) => {
const ranges: {[key: string ]: [number, number?]} = {
'zero': [0, breakpoints.s],
's': [breakpoints.s, breakpoints.m],
'm': [breakpoints.m, breakpoints.l],
'l': [breakpoints.l, breakpoints.xl],
'xl': [breakpoints.xl]
};
return breakpointKeys.find((size: BreakpointKeys) => isWithinBreakpoint(width, ...ranges[size])) || 'zero';
}
// Returns responsive style objects that are within the current CanvasBreakpoint size
function getStyles<T>(key: BreakpointKeys, styles: ResponsiveCSSObject<T>) {
const responsiveStyles = {} as CSSObject<T>;
const breakpointSize = breakpointKeys.indexOf(key);
for (let i = 0; i <= breakpointSize; i++) {
const breakpointName = breakpointKeys[i];
// property is key of the style object
Object.keys(styles).forEach((property) => {
const { zero, s, m, l, xl, ...base } = styles[property as keyof T];
const currentBreakpointStyles =
styles[property as keyof T][breakpointName] ?? {};
const previousBreakpointStyles = responsiveStyles[property as keyof T] ?? {};
responsiveStyles[property as keyof T] = {
...base,
...previousBreakpointStyles,
...currentBreakpointStyles
};
});
}
return responsiveStyles;
}
/**
* useResponsiveContainerStyles
*
* ---
*
* This hook allows you to create container-based responsive styles with objects.
* It accepts two or three arguments: the responsive style object, the container width, and (optionally) the theme object.
* Each style object accepts five breakpoint keys: "zero", "s", "m", "l", and "xl".
* The sizes will act like `min-width`. For example, if you want to apply styles from `medium` and up, then you would write those styles under `m`.
*
* @example
* ```tsx
import {Flex, Box} from '@workday/canvas-kit-react/layout';
import {
useResponsiveStyles,
} from '@workday/canvas-kit-react/common';
const ref = React.useRef(null);
const [containerWidth, setContainerWidth] = React.useState(0);
useResizeObserver({
ref: ref,
onResize: data => {
setWidth(data.width || 0);
},
});
const containerResponsiveStyles = useResponsiveStyles(
{
flex: {
flexDirection: 'column',
padding: 'm',
depth: 1,
borderRadius: 'l',
zero: {
backgroundColor: 'Red',
},
s: {
backgroundColor: 'Orange',
},
m: {
backgroundColor: 'Yellow',
},
l: {
backgroundColor: 'Green',
},
xl: {
backgroundColor: 'Blue',
},
},
box: {
padding: 's',
},
},
containerWidth
);
return (
<Box ref={ref}>
<Flex {...containerResponsiveStyles.flex}>
<Box {...containerResponsiveStyles.box}>Hello World</Box>
</Flex>
</Box>
);
```
*/
export function useResponsiveContainerStyles<T extends ResponsiveCSSObject<T>>(
styles: ResponsiveCSSObject<T>,
width: number,
theme = {}
) {
const canvasTheme = useTheme(theme);
const breakpoints = canvasTheme.canvas.breakpoints.values;
const size = getSize(width, breakpoints);
return getStyles(size, styles);
}