@khanacademy/wonder-blocks-button 2.11.7 → 3.0.0
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 +6 -0
- package/dist/es/index.js +12 -9
- package/dist/index.js +25 -20
- package/package.json +1 -1
- package/src/__tests__/__snapshots__/custom-snapshot.test.js.snap +4646 -780
- package/src/__tests__/custom-snapshot.test.js +1 -1
- package/src/components/__docs__/accessibility.stories.mdx +1 -1
- package/src/components/__docs__/best-practices.stories.mdx +1 -1
- package/src/components/__docs__/button.argtypes.js +3 -3
- package/src/components/{button.stories.js → __docs__/button.stories.js} +195 -275
- package/src/components/__docs__/navigation-callbacks.stories.mdx +121 -2
- package/src/components/__tests__/button.test.js +138 -191
- package/src/components/button-core.js +14 -8
- package/src/components/button.js +2 -2
- package/src/components/button.md +4 -920
- package/src/__tests__/__snapshots__/generated-snapshot.test.js.snap +0 -5009
- package/src/__tests__/generated-snapshot.test.js +0 -789
|
@@ -4,8 +4,10 @@ import {StyleSheet} from "aphrodite";
|
|
|
4
4
|
import Button from "@khanacademy/wonder-blocks-button";
|
|
5
5
|
import {View} from "@khanacademy/wonder-blocks-core";
|
|
6
6
|
|
|
7
|
+
import {styles} from "./button.stories.js";
|
|
8
|
+
|
|
7
9
|
<Meta
|
|
8
|
-
title="
|
|
10
|
+
title="Button / Navigation Callbacks"
|
|
9
11
|
component={Button}
|
|
10
12
|
parameters={{
|
|
11
13
|
previewTabs: {
|
|
@@ -19,7 +21,7 @@ import {View} from "@khanacademy/wonder-blocks-core";
|
|
|
19
21
|
}}
|
|
20
22
|
/>
|
|
21
23
|
|
|
22
|
-
|
|
24
|
+
# Running Callbacks on Navigation
|
|
23
25
|
|
|
24
26
|
Sometimes you may need to run some code and also navigate when the user
|
|
25
27
|
clicks the button. For example, you might want to send a request to the
|
|
@@ -66,3 +68,120 @@ rejects, `safeWithNav` will not be run.
|
|
|
66
68
|
|
|
67
69
|
If the `onClick` handler calls `preventDefault()`, then `beforeNav`
|
|
68
70
|
and `safeWithNav` will still run, but navigation will not occur.
|
|
71
|
+
|
|
72
|
+
export const BeforeNavCallbacks = () => (
|
|
73
|
+
<MemoryRouter>
|
|
74
|
+
<View style={styles.row}>
|
|
75
|
+
<Button
|
|
76
|
+
href="/foo"
|
|
77
|
+
style={styles.button}
|
|
78
|
+
beforeNav={() =>
|
|
79
|
+
new Promise((resolve, reject) => {
|
|
80
|
+
setTimeout(resolve, 1000);
|
|
81
|
+
})
|
|
82
|
+
}
|
|
83
|
+
>
|
|
84
|
+
beforeNav, client-side nav
|
|
85
|
+
</Button>
|
|
86
|
+
<Button
|
|
87
|
+
href="/foo"
|
|
88
|
+
style={styles.button}
|
|
89
|
+
skipClientNav={true}
|
|
90
|
+
beforeNav={() =>
|
|
91
|
+
new Promise((resolve, reject) => {
|
|
92
|
+
setTimeout(resolve, 1000);
|
|
93
|
+
})
|
|
94
|
+
}
|
|
95
|
+
>
|
|
96
|
+
beforeNav, server-side nav
|
|
97
|
+
</Button>
|
|
98
|
+
<Button
|
|
99
|
+
href="https://google.com"
|
|
100
|
+
style={styles.button}
|
|
101
|
+
skipClientNav={true}
|
|
102
|
+
beforeNav={() =>
|
|
103
|
+
new Promise((resolve, reject) => {
|
|
104
|
+
setTimeout(resolve, 1000);
|
|
105
|
+
})
|
|
106
|
+
}
|
|
107
|
+
>
|
|
108
|
+
beforeNav, open URL in new tab
|
|
109
|
+
</Button>
|
|
110
|
+
<Switch>
|
|
111
|
+
<Route path="/foo">
|
|
112
|
+
<View id="foo">Hello, world!</View>
|
|
113
|
+
</Route>
|
|
114
|
+
</Switch>
|
|
115
|
+
</View>
|
|
116
|
+
</MemoryRouter>
|
|
117
|
+
);
|
|
118
|
+
|
|
119
|
+
export const SafeWithNavCallbacks = () => (
|
|
120
|
+
<MemoryRouter>
|
|
121
|
+
<View style={styles.row}>
|
|
122
|
+
<Button
|
|
123
|
+
href="/foo"
|
|
124
|
+
style={styles.button}
|
|
125
|
+
safeWithNav={() =>
|
|
126
|
+
new Promise((resolve, reject) => {
|
|
127
|
+
setTimeout(resolve, 1000);
|
|
128
|
+
})
|
|
129
|
+
}
|
|
130
|
+
>
|
|
131
|
+
safeWithNav, client-side nav
|
|
132
|
+
</Button>
|
|
133
|
+
<Button
|
|
134
|
+
href="/foo"
|
|
135
|
+
style={styles.button}
|
|
136
|
+
skipClientNav={true}
|
|
137
|
+
safeWithNav={() =>
|
|
138
|
+
new Promise((resolve, reject) => {
|
|
139
|
+
setTimeout(resolve, 1000);
|
|
140
|
+
})
|
|
141
|
+
}
|
|
142
|
+
>
|
|
143
|
+
safeWithNav, server-side nav
|
|
144
|
+
</Button>
|
|
145
|
+
<Button
|
|
146
|
+
href="https://google.com"
|
|
147
|
+
style={styles.button}
|
|
148
|
+
skipClientNav={true}
|
|
149
|
+
safeWithNav={() =>
|
|
150
|
+
new Promise((resolve, reject) => {
|
|
151
|
+
setTimeout(resolve, 1000);
|
|
152
|
+
})
|
|
153
|
+
}
|
|
154
|
+
>
|
|
155
|
+
safeWithNav, open URL in new tab
|
|
156
|
+
</Button>
|
|
157
|
+
<Switch>
|
|
158
|
+
<Route path="/foo">
|
|
159
|
+
<View id="foo">Hello, world!</View>
|
|
160
|
+
</Route>
|
|
161
|
+
</Switch>
|
|
162
|
+
</View>
|
|
163
|
+
</MemoryRouter>
|
|
164
|
+
);
|
|
165
|
+
|
|
166
|
+
## Stories
|
|
167
|
+
|
|
168
|
+
### beforeNav Callbacks
|
|
169
|
+
|
|
170
|
+
These buttons always wait until the async callback code completes before
|
|
171
|
+
starting navigation.
|
|
172
|
+
|
|
173
|
+
<Canvas>
|
|
174
|
+
<Story name="beforeNav Callbacks">
|
|
175
|
+
{BeforeNavCallbacks.bind({})}
|
|
176
|
+
</Story>
|
|
177
|
+
</Canvas>
|
|
178
|
+
|
|
179
|
+
### safeWithNav Callbacks
|
|
180
|
+
|
|
181
|
+
If the `onClick` callback calls `preventDefault()`, then navigation will not occur.
|
|
182
|
+
|
|
183
|
+
<Canvas>
|
|
184
|
+
<Story name="safeWithNav Callbacks">
|
|
185
|
+
{SafeWithNavCallbacks.bind({})}
|
|
186
|
+
</Story>
|
|
187
|
+
</Canvas>
|