@khanacademy/wonder-blocks-layout 1.4.11 → 1.4.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md
CHANGED
package/docs.md
CHANGED
|
@@ -1,248 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
Documentation for `@khanacademy/wonder-blocks-layout` is now in Storybook.
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
```js
|
|
8
|
-
import {StyleSheet} from "aphrodite";
|
|
9
|
-
import {View} from "@khanacademy/wonder-blocks-core";
|
|
10
|
-
import Color from "@khanacademy/wonder-blocks-color";
|
|
11
|
-
import Spacing from "@khanacademy/wonder-blocks-spacing";
|
|
12
|
-
import Button from "@khanacademy/wonder-blocks-button";
|
|
13
|
-
import {Spring, Strut} from "@khanacademy/wonder-blocks-layout";
|
|
14
|
-
|
|
15
|
-
const styles = StyleSheet.create({
|
|
16
|
-
container: {
|
|
17
|
-
flexDirection: "row",
|
|
18
|
-
border: `solid 1px ${Color.offBlack50}`,
|
|
19
|
-
},
|
|
20
|
-
button: {
|
|
21
|
-
width: 100,
|
|
22
|
-
},
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
<View style={styles.container}>
|
|
26
|
-
<Button style={styles.button}>A</Button>
|
|
27
|
-
<Strut size={Spacing.small_12}/>
|
|
28
|
-
<Button style={styles.button}>B</Button>
|
|
29
|
-
<Strut size={Spacing.small_12}/>
|
|
30
|
-
<Button style={styles.button}>C</Button>
|
|
31
|
-
<Spring/>
|
|
32
|
-
<Button style={styles.button}>Cancel</Button>
|
|
33
|
-
<Strut size={Spacing.small_12}/>
|
|
34
|
-
<Button style={styles.button}>Accept</Button>
|
|
35
|
-
</View>
|
|
36
|
-
```
|
|
37
|
-
|
|
38
|
-
## MediaLayout
|
|
39
|
-
|
|
40
|
-
`MediaLayout` is a container component that accepts a `styleSheets` object, whose keys are
|
|
41
|
-
media sizes. It listens for changes to the current media size and passes the current
|
|
42
|
-
`mediaSize`, `mediaSpec`, and `styles` to `children`, which is a render function taking
|
|
43
|
-
those three values as an object.
|
|
44
|
-
|
|
45
|
-
Valid keys for the `styleSheets` object are (in order of precedence):
|
|
46
|
-
- `small`, `medium`, `large`
|
|
47
|
-
- `mdOrSmaller`, `mdOrLarger`
|
|
48
|
-
- `all`
|
|
49
|
-
|
|
50
|
-
`MediaLayout` will merge style rules from multiple styles that match the current media
|
|
51
|
-
query, e.g. `"(min-width: 1024px)"`.
|
|
52
|
-
|
|
53
|
-
The `mediaSpec` is an object with one or more of the following keys: `small`, `medium`,
|
|
54
|
-
or `large`. Each value contains the following data:
|
|
55
|
-
- `query: string` e.g. "(min-width: 1024px)"
|
|
56
|
-
- `totalColumns: number`
|
|
57
|
-
- `gutterWidth: number`
|
|
58
|
-
- `marginWidth: number`
|
|
59
|
-
- `maxWidth: number`
|
|
60
|
-
|
|
61
|
-
### Examples
|
|
62
|
-
|
|
63
|
-
#### 1. Switching styles for different screen sizes
|
|
64
|
-
|
|
65
|
-
By default, `MediaLayout` uses `MEDIA_DEFAULT_SPEC`. Here you can see an
|
|
66
|
-
example that changes styles depending on the current spec.
|
|
67
|
-
|
|
68
|
-
```js
|
|
69
|
-
import {StyleSheet} from "aphrodite";
|
|
70
|
-
import Color from "@khanacademy/wonder-blocks-color";
|
|
71
|
-
import {View} from "@khanacademy/wonder-blocks-core";
|
|
72
|
-
import {MediaLayout} from "@khanacademy/wonder-blocks-layout";
|
|
73
|
-
|
|
74
|
-
const styleSheets = {
|
|
75
|
-
large: StyleSheet.create({
|
|
76
|
-
test: {
|
|
77
|
-
backgroundColor: Color.darkBlue,
|
|
78
|
-
color: Color.white,
|
|
79
|
-
},
|
|
80
|
-
}),
|
|
81
|
-
medium: StyleSheet.create({
|
|
82
|
-
test: {
|
|
83
|
-
backgroundColor: Color.blue,
|
|
84
|
-
color: Color.white,
|
|
85
|
-
},
|
|
86
|
-
}),
|
|
87
|
-
small: StyleSheet.create({
|
|
88
|
-
test: {
|
|
89
|
-
backgroundColor: Color.lightBlue,
|
|
90
|
-
color: Color.white,
|
|
91
|
-
},
|
|
92
|
-
}),
|
|
93
|
-
};
|
|
94
|
-
|
|
95
|
-
<MediaLayout styleSheets={styleSheets}>
|
|
96
|
-
{({mediaSize, mediaSpec, styles}) => {
|
|
97
|
-
return <View style={styles.test}>Hello, world!</View>;
|
|
98
|
-
}}
|
|
99
|
-
</MediaLayout>
|
|
100
|
-
```
|
|
101
|
-
|
|
102
|
-
#### 2. Defining shared styles for all sizes
|
|
103
|
-
|
|
104
|
-
You can use the `all` key to define styles for all the different sizes. This
|
|
105
|
-
means that by using this key, all the sizes (small, medium, large) will use the
|
|
106
|
-
styles defined in `all`, and in case there are duplicate properties, more
|
|
107
|
-
specific sizes will take more importance.
|
|
108
|
-
|
|
109
|
-
```js
|
|
110
|
-
import {StyleSheet} from "aphrodite";
|
|
111
|
-
import Color from "@khanacademy/wonder-blocks-color";
|
|
112
|
-
import {View} from "@khanacademy/wonder-blocks-core";
|
|
113
|
-
import Spacing from "@khanacademy/wonder-blocks-spacing";
|
|
114
|
-
import {MediaLayout} from "@khanacademy/wonder-blocks-layout";
|
|
115
|
-
|
|
116
|
-
const styleSheets = {
|
|
117
|
-
all: StyleSheet.create({
|
|
118
|
-
// use shared styles for all sizes
|
|
119
|
-
test: {
|
|
120
|
-
color: Color.white,
|
|
121
|
-
padding: Spacing.medium_16,
|
|
122
|
-
},
|
|
123
|
-
}),
|
|
124
|
-
|
|
125
|
-
large: StyleSheet.create({
|
|
126
|
-
// override the `padding` prop` here
|
|
127
|
-
test: {
|
|
128
|
-
backgroundColor: Color.darkBlue,
|
|
129
|
-
padding: Spacing.xxLarge_48,
|
|
130
|
-
},
|
|
131
|
-
}),
|
|
132
|
-
|
|
133
|
-
medium: StyleSheet.create({
|
|
134
|
-
test: {
|
|
135
|
-
backgroundColor: Color.blue,
|
|
136
|
-
},
|
|
137
|
-
}),
|
|
138
|
-
|
|
139
|
-
small: StyleSheet.create({
|
|
140
|
-
test: {
|
|
141
|
-
backgroundColor: Color.lightBlue,
|
|
142
|
-
},
|
|
143
|
-
}),
|
|
144
|
-
};
|
|
145
|
-
|
|
146
|
-
<MediaLayout styleSheets={styleSheets}>
|
|
147
|
-
{({styles}) => {
|
|
148
|
-
return <View style={styles.test}>Hello, world!</View>;
|
|
149
|
-
}}
|
|
150
|
-
</MediaLayout>
|
|
151
|
-
```
|
|
152
|
-
|
|
153
|
-
#### 3. Using a custom spec:
|
|
154
|
-
|
|
155
|
-
There are cases when you might need to use a custom media query spec. For that
|
|
156
|
-
situation you can define your own custom `MEDIA_SPEC`. You need to use the
|
|
157
|
-
`MediaLayoutContext.Provider` to specify this spec value.
|
|
158
|
-
|
|
159
|
-
**NOTE:** Make sure to import the `MediaSpec` and `MediaLayoutContextValue` type
|
|
160
|
-
definitions:
|
|
161
|
-
|
|
162
|
-
```js static
|
|
163
|
-
// 1. Import the required types
|
|
164
|
-
import type {MediaSpec, MediaLayoutContextValue} from "@khanacademy/wonder-blocks-layout";
|
|
165
|
-
|
|
166
|
-
// 2. Add the `MediaSpect` type to the MEDIA_CUSTOM_SPEC object
|
|
167
|
-
const MEDIA_CUSTOM_SPEC: MediaSpec = {
|
|
168
|
-
...
|
|
169
|
-
};
|
|
170
|
-
|
|
171
|
-
// 3. Make sure to add the type `MediaLayoutContextValue`
|
|
172
|
-
const contextValue: MediaLayoutContextValue = {
|
|
173
|
-
ssrSize: "large",
|
|
174
|
-
mediaSpec: MEDIA_CUSTOM_SPEC,
|
|
175
|
-
};
|
|
176
|
-
```
|
|
177
|
-
|
|
178
|
-
```js
|
|
179
|
-
import {StyleSheet} from "aphrodite";
|
|
180
|
-
import {View} from "@khanacademy/wonder-blocks-core";
|
|
181
|
-
import {Body, HeadingLarge, HeadingSmall} from "@khanacademy/wonder-blocks-typography";
|
|
182
|
-
import Color from "@khanacademy/wonder-blocks-color";
|
|
183
|
-
import Spacing from "@khanacademy/wonder-blocks-spacing";
|
|
184
|
-
import {MediaLayout, MediaLayoutContext} from "@khanacademy/wonder-blocks-layout";
|
|
185
|
-
|
|
186
|
-
// If you're using flow, make sure to import these types by uncommenting the following line
|
|
187
|
-
// import type {MediaSpec, MediaLayoutContextValue} from "@khanacademy/wonder-blocks-layout";
|
|
188
|
-
|
|
189
|
-
const styleSheets = {
|
|
190
|
-
large: StyleSheet.create({
|
|
191
|
-
example: {
|
|
192
|
-
alignItems: "center",
|
|
193
|
-
backgroundColor: Color.darkBlue,
|
|
194
|
-
color: Color.white,
|
|
195
|
-
padding: Spacing.xxxLarge_64,
|
|
196
|
-
},
|
|
197
|
-
}),
|
|
198
|
-
|
|
199
|
-
small: StyleSheet.create({
|
|
200
|
-
example: {
|
|
201
|
-
backgroundColor: Color.lightBlue,
|
|
202
|
-
padding: Spacing.small_12,
|
|
203
|
-
},
|
|
204
|
-
}),
|
|
205
|
-
};
|
|
206
|
-
|
|
207
|
-
// Custom media spec definition
|
|
208
|
-
// Make sure to add the type `MediaSpec`
|
|
209
|
-
const MEDIA_CUSTOM_SPEC = {
|
|
210
|
-
small: {
|
|
211
|
-
query: "(max-width: 767px)",
|
|
212
|
-
totalColumns: 4,
|
|
213
|
-
gutterWidth: Spacing.medium_16,
|
|
214
|
-
marginWidth: Spacing.medium_16,
|
|
215
|
-
},
|
|
216
|
-
large: {
|
|
217
|
-
query: "(min-width: 768px)",
|
|
218
|
-
totalColumns: 12,
|
|
219
|
-
gutterWidth: Spacing.xLarge_32,
|
|
220
|
-
marginWidth: Spacing.xxLarge_48,
|
|
221
|
-
},
|
|
222
|
-
};
|
|
223
|
-
|
|
224
|
-
// Make sure to add the type `MediaLayoutContextValue`
|
|
225
|
-
const contextValue = {
|
|
226
|
-
ssrSize: "large",
|
|
227
|
-
mediaSpec: MEDIA_CUSTOM_SPEC,
|
|
228
|
-
};
|
|
229
|
-
|
|
230
|
-
<MediaLayoutContext.Provider value={contextValue}>
|
|
231
|
-
<MediaLayout styleSheets={styleSheets}>
|
|
232
|
-
{({mediaSize, styles}) => {
|
|
233
|
-
const HeadingComponent = (mediaSize === "small") ? HeadingSmall : HeadingLarge;
|
|
234
|
-
|
|
235
|
-
return (
|
|
236
|
-
<View style={styles.example}>
|
|
237
|
-
<HeadingComponent>Current mediaSpec: {mediaSize}</HeadingComponent>
|
|
238
|
-
<Body tag="p">
|
|
239
|
-
{
|
|
240
|
-
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."
|
|
241
|
-
}
|
|
242
|
-
</Body>
|
|
243
|
-
</View>
|
|
244
|
-
);
|
|
245
|
-
}}
|
|
246
|
-
</MediaLayout>
|
|
247
|
-
</MediaLayoutContext.Provider>
|
|
248
|
-
```
|
|
3
|
+
Visit the [Storybook
|
|
4
|
+
Layout](https://khan.github.io/wonder-blocks/?path=/docs/layout) docs on GitHub
|
|
5
|
+
Pages.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@khanacademy/wonder-blocks-layout",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.12",
|
|
4
4
|
"design": "v1",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
},
|
|
15
15
|
"dependencies": {
|
|
16
16
|
"@babel/runtime": "^7.18.6",
|
|
17
|
-
"@khanacademy/wonder-blocks-core": "^4.
|
|
17
|
+
"@khanacademy/wonder-blocks-core": "^4.5.0",
|
|
18
18
|
"@khanacademy/wonder-blocks-spacing": "^3.0.5"
|
|
19
19
|
},
|
|
20
20
|
"devDependencies": {
|