@instructure/ui-skeleton 0.0.0-stage → 0.0.1-bootstrap-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.
Files changed (79) hide show
  1. package/CHANGELOG.md +0 -0
  2. package/LICENSE.md +28 -0
  3. package/README.md +50 -2
  4. package/es/SkeletonCircle/index.js +68 -0
  5. package/es/SkeletonCircle/props.js +26 -0
  6. package/es/SkeletonCircle/styles.js +47 -0
  7. package/es/SkeletonLoader/index.js +104 -0
  8. package/es/SkeletonLoader/props.js +26 -0
  9. package/es/SkeletonLoader/styles.js +45 -0
  10. package/es/SkeletonRectangle/index.js +74 -0
  11. package/es/SkeletonRectangle/props.js +26 -0
  12. package/es/SkeletonRectangle/styles.js +52 -0
  13. package/es/SkeletonText/index.js +76 -0
  14. package/es/SkeletonText/props.js +26 -0
  15. package/es/SkeletonText/styles.js +95 -0
  16. package/es/index.js +25 -0
  17. package/es/utils/shimmer.js +81 -0
  18. package/lib/SkeletonCircle/index.js +75 -0
  19. package/lib/SkeletonCircle/props.js +31 -0
  20. package/lib/SkeletonCircle/styles.js +53 -0
  21. package/lib/SkeletonLoader/index.js +110 -0
  22. package/lib/SkeletonLoader/props.js +31 -0
  23. package/lib/SkeletonLoader/styles.js +51 -0
  24. package/lib/SkeletonRectangle/index.js +81 -0
  25. package/lib/SkeletonRectangle/props.js +31 -0
  26. package/lib/SkeletonRectangle/styles.js +58 -0
  27. package/lib/SkeletonText/index.js +83 -0
  28. package/lib/SkeletonText/props.js +31 -0
  29. package/lib/SkeletonText/styles.js +101 -0
  30. package/lib/index.js +12 -0
  31. package/lib/package.json +1 -0
  32. package/lib/utils/shimmer.js +87 -0
  33. package/package.json +70 -3
  34. package/src/SkeletonCircle/index.tsx +71 -0
  35. package/src/SkeletonCircle/props.ts +58 -0
  36. package/src/SkeletonCircle/styles.ts +61 -0
  37. package/src/SkeletonLoader/README.md +237 -0
  38. package/src/SkeletonLoader/index.tsx +126 -0
  39. package/src/SkeletonLoader/props.ts +100 -0
  40. package/src/SkeletonLoader/styles.ts +51 -0
  41. package/src/SkeletonRectangle/index.tsx +71 -0
  42. package/src/SkeletonRectangle/props.ts +81 -0
  43. package/src/SkeletonRectangle/styles.ts +69 -0
  44. package/src/SkeletonText/index.tsx +74 -0
  45. package/src/SkeletonText/props.ts +91 -0
  46. package/src/SkeletonText/styles.ts +115 -0
  47. package/src/index.ts +38 -0
  48. package/src/utils/shimmer.ts +101 -0
  49. package/tsconfig.build.json +32 -0
  50. package/tsconfig.build.tsbuildinfo +1 -0
  51. package/tsconfig.json +4 -0
  52. package/types/SkeletonCircle/index.d.ts +18 -0
  53. package/types/SkeletonCircle/index.d.ts.map +1 -0
  54. package/types/SkeletonCircle/props.d.ts +23 -0
  55. package/types/SkeletonCircle/props.d.ts.map +1 -0
  56. package/types/SkeletonCircle/styles.d.ts +13 -0
  57. package/types/SkeletonCircle/styles.d.ts.map +1 -0
  58. package/types/SkeletonLoader/index.d.ts +27 -0
  59. package/types/SkeletonLoader/index.d.ts.map +1 -0
  60. package/types/SkeletonLoader/props.d.ts +52 -0
  61. package/types/SkeletonLoader/props.d.ts.map +1 -0
  62. package/types/SkeletonLoader/styles.d.ts +11 -0
  63. package/types/SkeletonLoader/styles.d.ts.map +1 -0
  64. package/types/SkeletonRectangle/index.d.ts +18 -0
  65. package/types/SkeletonRectangle/index.d.ts.map +1 -0
  66. package/types/SkeletonRectangle/props.d.ts +36 -0
  67. package/types/SkeletonRectangle/props.d.ts.map +1 -0
  68. package/types/SkeletonRectangle/styles.d.ts +16 -0
  69. package/types/SkeletonRectangle/styles.d.ts.map +1 -0
  70. package/types/SkeletonText/index.d.ts +17 -0
  71. package/types/SkeletonText/index.d.ts.map +1 -0
  72. package/types/SkeletonText/props.d.ts +43 -0
  73. package/types/SkeletonText/props.d.ts.map +1 -0
  74. package/types/SkeletonText/styles.d.ts +16 -0
  75. package/types/SkeletonText/styles.d.ts.map +1 -0
  76. package/types/index.d.ts +7 -0
  77. package/types/index.d.ts.map +1 -0
  78. package/types/utils/shimmer.d.ts +28 -0
  79. package/types/utils/shimmer.d.ts.map +1 -0
@@ -0,0 +1,237 @@
1
+ ---
2
+ describes: SkeletonLoader
3
+ ---
4
+
5
+ ### Controlled
6
+
7
+ Set `isLoading` when you're waiting for data and know when it arrives.
8
+
9
+ ```js
10
+ ---
11
+ type: example
12
+ ---
13
+ const Example = () => {
14
+ const [isLoading, setIsLoading] = useState(true)
15
+
16
+ return (
17
+ <div>
18
+ <Button
19
+ onClick={() => setIsLoading(!isLoading)}
20
+ margin="0 0 general.spaceMd 0"
21
+ >
22
+ Toggle
23
+ </Button>
24
+ <SkeletonLoader
25
+ isLoading={isLoading}
26
+ loadingLabel="Loading courses"
27
+ loadedLabel="3 courses"
28
+ skeleton={<SkeletonLoader.Text lines={3} />}
29
+ >
30
+ <List>
31
+ <List.Item>Biology 101</List.Item>
32
+ <List.Item>Chemistry 201</List.Item>
33
+ <List.Item>Physics 301</List.Item>
34
+ </List>
35
+ </SkeletonLoader>
36
+ </div>
37
+ )
38
+ }
39
+
40
+ render(<Example />)
41
+ ```
42
+
43
+ #### When the request fails
44
+
45
+ Set `isError` and `isLoading={false}`, and render your error message as
46
+ `children`. `SkeletonLoader` announces `errorLabel` to screen readers right
47
+ away, but the message, its wording, and any retry button are up to you.
48
+
49
+ Store the error in state instead of checking for an empty result. An empty list
50
+ and a failed request need different messages: "No courses yet" isn't the same as
51
+ "We couldn't load your courses."
52
+
53
+ ```js
54
+ ---
55
+ type: example
56
+ ---
57
+ const Example = () => {
58
+ const [isLoading, setIsLoading] = useState(false)
59
+ const [error, setError] = useState(null)
60
+ const [courses, setCourses] = useState([])
61
+
62
+ const load = async ({ shouldFail }) => {
63
+ setIsLoading(true)
64
+ setError(null)
65
+
66
+ try {
67
+ const result = await new Promise((resolve, reject) =>
68
+ setTimeout(
69
+ () =>
70
+ shouldFail
71
+ ? reject(new Error('Network request failed'))
72
+ : resolve(['Biology 101', 'Chemistry 201', 'Physics 301']),
73
+ 1200
74
+ )
75
+ )
76
+ setCourses(result)
77
+ } catch (err) {
78
+ setError(err)
79
+ setCourses([])
80
+ } finally {
81
+ setIsLoading(false)
82
+ }
83
+ }
84
+
85
+ return (
86
+ <div>
87
+ <Button
88
+ onClick={() => load({ shouldFail: false })}
89
+ margin="0 general.spaceSm general.spaceMd 0"
90
+ >
91
+ Load
92
+ </Button>
93
+ <Button
94
+ color="danger"
95
+ onClick={() => load({ shouldFail: true })}
96
+ margin="0 0 general.spaceMd 0"
97
+ >
98
+ Load and fail
99
+ </Button>
100
+
101
+ <SkeletonLoader
102
+ isLoading={isLoading}
103
+ isError={Boolean(error)}
104
+ loadingLabel="Loading courses"
105
+ loadedLabel={`${courses.length} courses`}
106
+ errorLabel="Couldn't load courses."
107
+ skeleton={<SkeletonLoader.Text lines={3} />}
108
+ >
109
+ {error ? (
110
+ <Alert variant="error" margin="0">
111
+ We couldn't load your courses.{' '}
112
+ <Link onClick={() => load({ shouldFail: false })}>Try again</Link>
113
+ </Alert>
114
+ ) : (
115
+ <List>
116
+ {courses.map((course) => (
117
+ <List.Item key={course}>{course}</List.Item>
118
+ ))}
119
+ </List>
120
+ )}
121
+ </SkeletonLoader>
122
+ </div>
123
+ )
124
+ }
125
+
126
+ render(<Example />)
127
+ ```
128
+
129
+ If `isError` is set, the skeleton is hidden even when `isLoading` is still
130
+ `true`.
131
+
132
+ ### Following hydration
133
+
134
+ If you don't set `isLoading`, the server renders the skeleton and `children`
135
+ replace it once the page has hydrated. Use this for content that can only
136
+ render in the browser.
137
+
138
+ ### Announcements
139
+
140
+ `loadingLabel` is announced only if loading takes longer than
141
+ `announcementDelay` (400ms by default). `loadedLabel` is announced when loading
142
+ finishes. Both are required, so they can be translated. If there are no
143
+ results, put the empty message in `loadedLabel`.
144
+
145
+ `errorLabel` is announced right away with `role="alert"`.
146
+
147
+ Focus doesn't move when the content loads.
148
+
149
+ ## Shapes
150
+
151
+ Build the `skeleton` from three shapes. All of them are hidden from screen
152
+ readers, so one `SkeletonLoader` can hold as many as you need and still
153
+ announce once.
154
+
155
+ - `SkeletonLoader.Text` for lines of text
156
+ - `SkeletonLoader.Rectangle` for images, video, and other media
157
+ - `SkeletonLoader.Circle` for avatars
158
+
159
+ Shapes are sized with CSS only, so they render at the right size in the server
160
+ HTML, before any JavaScript runs. When they match the size of the real content,
161
+ the page doesn't jump when it loads.
162
+
163
+ ### Text
164
+
165
+ `size` is the font size of the text being replaced, and `lines` is the number
166
+ of lines. The last line is shorter so it reads as a paragraph.
167
+
168
+ ```js
169
+ ---
170
+ type: example
171
+ ---
172
+ <div>
173
+ <SkeletonLoader.Text size="xl" />
174
+ <br />
175
+ <SkeletonLoader.Text size="md" lines={3} />
176
+ </div>
177
+ ```
178
+
179
+ ### Line height
180
+
181
+ Set `lineHeight` to match the text you're replacing: `heading` (1.25) for
182
+ headings, or `text` (1.5) for body copy. If it doesn't match, the skeleton will
183
+ be a different height from the content and the page will jump when it loads.
184
+
185
+ ```js
186
+ ---
187
+ type: example
188
+ ---
189
+ <div>
190
+ <SkeletonLoader.Text size="md" lines={3} lineHeight="heading" />
191
+ <br />
192
+ <SkeletonLoader.Text size="md" lines={3} lineHeight="text" />
193
+ </div>
194
+ ```
195
+
196
+ ### Rectangle and Circle
197
+
198
+ A `Rectangle` uses `aspectRatio` by default (16 / 9), so it scales with the
199
+ width of its container. Set `height` for a fixed height instead. A `Circle` is
200
+ sized by its `diameter`.
201
+
202
+ ```js
203
+ ---
204
+ type: example
205
+ ---
206
+ <div style={{ display: 'flex', gap: '1rem', alignItems: 'flex-start' }}>
207
+ <SkeletonLoader.Circle diameter="3rem" />
208
+ <div style={{ flex: 1 }}>
209
+ <SkeletonLoader.Rectangle aspectRatio="16 / 9" />
210
+ </div>
211
+ <div style={{ flex: 1 }}>
212
+ <SkeletonLoader.Rectangle height="6rem" />
213
+ </div>
214
+ </div>
215
+ ```
216
+
217
+ ### Animation
218
+
219
+ A highlight sweeps across each shape, from the start of the line to the end,
220
+ until the content loads.
221
+
222
+ The animation only runs when the user hasn't turned on reduced motion in their
223
+ system settings. Since it loops for as long as the content is loading, that
224
+ setting is how users stop it (WCAG 2.2.2, Pause, Stop, Hide).
225
+
226
+ When a shape isn't animating, the highlight stays partway across it, so it
227
+ still looks like a skeleton rather than a flat gray block.
228
+
229
+ Set `animate={false}` to turn the animation off, for example in snapshot and
230
+ visual regression tests.
231
+
232
+ ```js
233
+ ---
234
+ type: example
235
+ ---
236
+ <SkeletonLoader.Text size="lg" lines={2} animate={false} />
237
+ ```
@@ -0,0 +1,126 @@
1
+ /*
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright (c) 2015 - present Instructure, Inc.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ import { useEffect, useState } from 'react'
26
+
27
+ import { useIsHydratedContext, omitProps } from '@instructure/ui-react-utils'
28
+
29
+ import generateStyle from './styles.js'
30
+ import { allowedProps } from './props.js'
31
+ import type { SkeletonLoaderProps } from './props'
32
+ import { SkeletonText } from '../SkeletonText'
33
+ import { SkeletonRectangle } from '../SkeletonRectangle'
34
+ import { SkeletonCircle } from '../SkeletonCircle'
35
+
36
+ /**
37
+ ---
38
+ category: components
39
+ ---
40
+ Shows a placeholder while part of the page is loading, and tells screen reader
41
+ users when loading starts and finishes.
42
+
43
+ Use one `SkeletonLoader` for each loading area, not one per card or row, so
44
+ screen readers announce it once.
45
+ @module SkeletonLoader
46
+ **/
47
+ function SkeletonLoader(props: SkeletonLoaderProps) {
48
+ const {
49
+ announcementDelay = 400,
50
+ children,
51
+ elementRef,
52
+ errorLabel,
53
+ isError = false,
54
+ isLoading,
55
+ loadedLabel,
56
+ loadingLabel,
57
+ skeleton
58
+ } = props
59
+
60
+ const isHydrated = useIsHydratedContext()
61
+
62
+ // If `isLoading` is set, it decides. If not, we're loading until hydration
63
+ // finishes. An error always ends loading, so the skeleton is never shown
64
+ // together with an error.
65
+ const loading = isError ? false : isLoading ?? !isHydrated
66
+
67
+ // Starts empty so screen readers treat the first label as a change and
68
+ // announce it.
69
+ const [status, setStatus] = useState('')
70
+
71
+ const styles = generateStyle()
72
+
73
+ useEffect(() => {
74
+ if (isError) {
75
+ setStatus('')
76
+ return
77
+ }
78
+
79
+ if (loading) {
80
+ const timeout = setTimeout(
81
+ () => setStatus(loadingLabel),
82
+ announcementDelay
83
+ )
84
+ return () => clearTimeout(timeout)
85
+ }
86
+
87
+ setStatus(loadedLabel)
88
+ return undefined
89
+ }, [loading, isError, loadingLabel, loadedLabel, announcementDelay])
90
+
91
+ return (
92
+ <>
93
+ <span
94
+ role="status"
95
+ aria-live="polite"
96
+ aria-atomic="true"
97
+ css={styles.srOnly}
98
+ >
99
+ {status}
100
+ </span>
101
+
102
+ <div
103
+ {...omitProps(props, allowedProps)}
104
+ ref={elementRef}
105
+ aria-busy={loading || undefined}
106
+ >
107
+ {loading ? skeleton : children}
108
+ </div>
109
+
110
+ {isError && errorLabel ? (
111
+ <span role="alert" css={styles.srOnly}>
112
+ {errorLabel}
113
+ </span>
114
+ ) : null}
115
+ </>
116
+ )
117
+ }
118
+
119
+ SkeletonLoader.displayName = 'SkeletonLoader'
120
+ SkeletonLoader.allowedProps = allowedProps
121
+ SkeletonLoader.Text = SkeletonText
122
+ SkeletonLoader.Rectangle = SkeletonRectangle
123
+ SkeletonLoader.Circle = SkeletonCircle
124
+
125
+ export default SkeletonLoader
126
+ export { SkeletonLoader }
@@ -0,0 +1,100 @@
1
+ /*
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright (c) 2015 - present Instructure, Inc.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ import type { ReactNode } from 'react'
26
+
27
+ import type { OtherHTMLAttributes } from '@instructure/shared-types'
28
+
29
+ type SkeletonShapeType = 'text' | 'rectangle' | 'circle'
30
+
31
+ type SkeletonLoaderOwnProps = {
32
+ /**
33
+ * How long to wait, in milliseconds, before announcing `loadingLabel`. If
34
+ * loading finishes before then, only `loadedLabel` is announced.
35
+ */
36
+ announcementDelay?: number
37
+
38
+ /**
39
+ * The content to show once loading has finished.
40
+ */
41
+ children?: ReactNode
42
+
43
+ /**
44
+ * Provides a reference to the underlying HTML element.
45
+ */
46
+ elementRef?: (element: Element | null) => void
47
+
48
+ /**
49
+ * Announced right away (with `role="alert"`) when `isError` is set.
50
+ */
51
+ errorLabel?: string
52
+
53
+ /**
54
+ * Set when loading failed. Stops loading and shows `children`, which should
55
+ * contain your error message.
56
+ */
57
+ isError?: boolean
58
+
59
+ /**
60
+ * Whether the content is loading. Set it yourself when you're waiting for
61
+ * data. If you leave it unset, the skeleton shows until the page has
62
+ * hydrated.
63
+ */
64
+ isLoading?: boolean
65
+
66
+ /**
67
+ * Announced when loading finishes. Required, so it can be translated.
68
+ */
69
+ loadedLabel: string
70
+
71
+ /**
72
+ * Announced after `announcementDelay` if the content is still loading.
73
+ * Required, so it can be translated.
74
+ */
75
+ loadingLabel: string
76
+
77
+ /**
78
+ * The placeholder to show while loading, built from `SkeletonLoader.Text`,
79
+ * `SkeletonLoader.Rectangle`, and `SkeletonLoader.Circle`.
80
+ */
81
+ skeleton?: ReactNode
82
+ }
83
+
84
+ type SkeletonLoaderProps = SkeletonLoaderOwnProps &
85
+ OtherHTMLAttributes<SkeletonLoaderOwnProps>
86
+
87
+ const allowedProps: (keyof SkeletonLoaderOwnProps)[] = [
88
+ 'announcementDelay',
89
+ 'children',
90
+ 'elementRef',
91
+ 'errorLabel',
92
+ 'isError',
93
+ 'isLoading',
94
+ 'loadedLabel',
95
+ 'loadingLabel',
96
+ 'skeleton'
97
+ ]
98
+
99
+ export { allowedProps }
100
+ export type { SkeletonLoaderProps, SkeletonLoaderOwnProps, SkeletonShapeType }
@@ -0,0 +1,51 @@
1
+ /*
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright (c) 2015 - present Instructure, Inc.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ import type { ComponentStyle } from '@instructure/emotion'
26
+
27
+ type SkeletonLoaderStyle = ComponentStyle<'srOnly'>
28
+
29
+ /**
30
+ * Visually hidden but still announced. Not `display: none`, which would remove
31
+ * the live region from the accessibility tree and stop it announcing at all.
32
+ */
33
+ const generateStyle = (): SkeletonLoaderStyle => ({
34
+ srOnly: {
35
+ label: 'skeletonLoader__srOnly',
36
+ position: 'absolute',
37
+ width: '1px',
38
+ height: '1px',
39
+ margin: '-1px',
40
+ padding: 0,
41
+ border: 0,
42
+ overflow: 'hidden',
43
+ clip: 'rect(0 0 0 0)',
44
+ clipPath: 'inset(50%)',
45
+ whiteSpace: 'nowrap'
46
+ }
47
+ })
48
+
49
+ export default generateStyle
50
+ export { generateStyle }
51
+ export type { SkeletonLoaderStyle }
@@ -0,0 +1,71 @@
1
+ /*
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright (c) 2015 - present Instructure, Inc.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ import { useContext } from 'react'
26
+
27
+ import { useStyleNew } from '@instructure/emotion'
28
+ import { TextDirectionContext } from '@instructure/ui-i18n'
29
+ import { omitProps } from '@instructure/ui-react-utils'
30
+
31
+ import generateStyle from './styles.js'
32
+ import { allowedProps } from './props.js'
33
+ import type { SkeletonLoaderTheme } from '../utils/shimmer'
34
+ import type { SkeletonRectangleProps } from './props'
35
+
36
+ /**
37
+ ---
38
+ parent: SkeletonLoader
39
+ id: SkeletonLoader.Rectangle
40
+ ---
41
+ A placeholder for images, video, and other media. It's hidden from screen
42
+ readers.
43
+ **/
44
+ function SkeletonRectangle(props: SkeletonRectangleProps) {
45
+ const { animate, aspectRatio, elementRef, height, radius, width } = props
46
+
47
+ const textDirection = useContext(TextDirectionContext)
48
+
49
+ const styles = useStyleNew<SkeletonLoaderTheme, typeof generateStyle>({
50
+ generateStyle,
51
+ componentId: 'SkeletonLoader',
52
+ displayName: 'SkeletonRectangle',
53
+ params: { animate, aspectRatio, height, radius, width, textDirection }
54
+ })
55
+
56
+ return (
57
+ <span
58
+ {...omitProps(props, allowedProps)}
59
+ ref={elementRef}
60
+ css={styles.skeletonRectangle}
61
+ aria-hidden="true"
62
+ data-skeleton-shape="rectangle"
63
+ />
64
+ )
65
+ }
66
+
67
+ SkeletonRectangle.displayName = 'SkeletonRectangle'
68
+ SkeletonRectangle.allowedProps = allowedProps
69
+
70
+ export default SkeletonRectangle
71
+ export { SkeletonRectangle }
@@ -0,0 +1,81 @@
1
+ /*
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright (c) 2015 - present Instructure, Inc.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ import type { OtherHTMLAttributes } from '@instructure/shared-types'
26
+ import type { ComponentStyle } from '@instructure/emotion'
27
+
28
+ type SkeletonRectangleOwnProps = {
29
+ /**
30
+ * Set to `false` to turn off the animation, whatever the user's motion
31
+ * setting. Useful for snapshot and visual regression tests.
32
+ */
33
+ animate?: boolean
34
+
35
+ /**
36
+ * Aspect ratio of the rectangle, e.g. `"16 / 9"`. Used when `height` isn't
37
+ * set, so the rectangle scales with the width of its container.
38
+ */
39
+ aspectRatio?: string
40
+
41
+ /**
42
+ * Provides a reference to the underlying HTML element.
43
+ */
44
+ elementRef?: (element: Element | null) => void
45
+
46
+ /**
47
+ * Fixed height. When set, `aspectRatio` is ignored.
48
+ */
49
+ height?: string | number
50
+
51
+ /**
52
+ * Corner radius. Defaults to the theme's image border radius.
53
+ */
54
+ radius?: string
55
+
56
+ /**
57
+ * Width of the rectangle. Defaults to the full width of its container.
58
+ */
59
+ width?: string | number
60
+ }
61
+
62
+ type SkeletonRectangleStyle = ComponentStyle<'skeletonRectangle'>
63
+
64
+ type SkeletonRectangleProps = SkeletonRectangleOwnProps &
65
+ OtherHTMLAttributes<SkeletonRectangleOwnProps>
66
+
67
+ const allowedProps: (keyof SkeletonRectangleOwnProps)[] = [
68
+ 'animate',
69
+ 'aspectRatio',
70
+ 'elementRef',
71
+ 'height',
72
+ 'radius',
73
+ 'width'
74
+ ]
75
+
76
+ export { allowedProps }
77
+ export type {
78
+ SkeletonRectangleOwnProps,
79
+ SkeletonRectangleProps,
80
+ SkeletonRectangleStyle
81
+ }