@instructure/ui-progress 11.6.0 → 11.6.1-snapshot-129

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 (97) hide show
  1. package/CHANGELOG.md +37 -294
  2. package/es/ProgressBar/{index.js → v1/index.js} +2 -2
  3. package/es/ProgressBar/v2/index.js +130 -0
  4. package/{src/index.ts → es/ProgressBar/v2/props.js} +2 -5
  5. package/es/ProgressBar/v2/styles.js +237 -0
  6. package/es/ProgressCircle/{index.js → v1/index.js} +2 -2
  7. package/es/ProgressCircle/v2/index.js +173 -0
  8. package/es/ProgressCircle/v2/props.js +26 -0
  9. package/es/ProgressCircle/v2/styles.js +349 -0
  10. package/es/{index.js → exports/a.js} +2 -2
  11. package/es/exports/b.js +25 -0
  12. package/lib/ProgressBar/{index.js → v1/index.js} +3 -3
  13. package/lib/ProgressBar/v2/index.js +136 -0
  14. package/lib/ProgressBar/v2/props.js +31 -0
  15. package/lib/ProgressBar/v2/styles.js +243 -0
  16. package/lib/ProgressCircle/{index.js → v1/index.js} +3 -3
  17. package/lib/ProgressCircle/v2/index.js +179 -0
  18. package/lib/ProgressCircle/v2/props.js +31 -0
  19. package/lib/ProgressCircle/v2/styles.js +355 -0
  20. package/lib/{index.js → exports/a.js} +4 -4
  21. package/lib/exports/b.js +19 -0
  22. package/package.json +40 -18
  23. package/src/ProgressBar/{index.tsx → v1/index.tsx} +2 -2
  24. package/src/ProgressBar/v2/README.md +384 -0
  25. package/src/ProgressBar/v2/index.tsx +142 -0
  26. package/src/ProgressBar/v2/props.ts +155 -0
  27. package/src/ProgressBar/v2/styles.ts +221 -0
  28. package/src/ProgressCircle/{index.tsx → v1/index.tsx} +2 -2
  29. package/src/ProgressCircle/v2/README.md +219 -0
  30. package/src/ProgressCircle/v2/index.tsx +196 -0
  31. package/src/ProgressCircle/v2/props.ts +141 -0
  32. package/src/ProgressCircle/v2/styles.ts +343 -0
  33. package/src/exports/a.ts +29 -0
  34. package/src/exports/b.ts +29 -0
  35. package/tsconfig.build.tsbuildinfo +1 -1
  36. package/types/ProgressBar/v1/index.d.ts.map +1 -0
  37. package/types/ProgressBar/v1/props.d.ts.map +1 -0
  38. package/types/ProgressBar/v1/styles.d.ts.map +1 -0
  39. package/types/ProgressBar/v1/theme.d.ts.map +1 -0
  40. package/types/ProgressBar/v2/index.d.ts +43 -0
  41. package/types/ProgressBar/v2/index.d.ts.map +1 -0
  42. package/types/ProgressBar/v2/props.d.ts +77 -0
  43. package/types/ProgressBar/v2/props.d.ts.map +1 -0
  44. package/types/ProgressBar/v2/styles.d.ts +15 -0
  45. package/types/ProgressBar/v2/styles.d.ts.map +1 -0
  46. package/types/ProgressCircle/v1/index.d.ts.map +1 -0
  47. package/types/ProgressCircle/v1/props.d.ts.map +1 -0
  48. package/types/ProgressCircle/v1/styles.d.ts.map +1 -0
  49. package/types/ProgressCircle/v1/theme.d.ts.map +1 -0
  50. package/types/ProgressCircle/v2/index.d.ts +49 -0
  51. package/types/ProgressCircle/v2/index.d.ts.map +1 -0
  52. package/types/ProgressCircle/v2/props.d.ts +75 -0
  53. package/types/ProgressCircle/v2/props.d.ts.map +1 -0
  54. package/types/ProgressCircle/v2/styles.d.ts +15 -0
  55. package/types/ProgressCircle/v2/styles.d.ts.map +1 -0
  56. package/types/exports/a.d.ts +5 -0
  57. package/types/exports/a.d.ts.map +1 -0
  58. package/types/exports/b.d.ts +5 -0
  59. package/types/exports/b.d.ts.map +1 -0
  60. package/types/ProgressBar/index.d.ts.map +0 -1
  61. package/types/ProgressBar/props.d.ts.map +0 -1
  62. package/types/ProgressBar/styles.d.ts.map +0 -1
  63. package/types/ProgressBar/theme.d.ts.map +0 -1
  64. package/types/ProgressCircle/index.d.ts.map +0 -1
  65. package/types/ProgressCircle/props.d.ts.map +0 -1
  66. package/types/ProgressCircle/styles.d.ts.map +0 -1
  67. package/types/ProgressCircle/theme.d.ts.map +0 -1
  68. package/types/index.d.ts +0 -5
  69. package/types/index.d.ts.map +0 -1
  70. /package/es/ProgressBar/{props.js → v1/props.js} +0 -0
  71. /package/es/ProgressBar/{styles.js → v1/styles.js} +0 -0
  72. /package/es/ProgressBar/{theme.js → v1/theme.js} +0 -0
  73. /package/es/ProgressCircle/{props.js → v1/props.js} +0 -0
  74. /package/es/ProgressCircle/{styles.js → v1/styles.js} +0 -0
  75. /package/es/ProgressCircle/{theme.js → v1/theme.js} +0 -0
  76. /package/lib/ProgressBar/{props.js → v1/props.js} +0 -0
  77. /package/lib/ProgressBar/{styles.js → v1/styles.js} +0 -0
  78. /package/lib/ProgressBar/{theme.js → v1/theme.js} +0 -0
  79. /package/lib/ProgressCircle/{props.js → v1/props.js} +0 -0
  80. /package/lib/ProgressCircle/{styles.js → v1/styles.js} +0 -0
  81. /package/lib/ProgressCircle/{theme.js → v1/theme.js} +0 -0
  82. /package/src/ProgressBar/{README.md → v1/README.md} +0 -0
  83. /package/src/ProgressBar/{props.ts → v1/props.ts} +0 -0
  84. /package/src/ProgressBar/{styles.ts → v1/styles.ts} +0 -0
  85. /package/src/ProgressBar/{theme.ts → v1/theme.ts} +0 -0
  86. /package/src/ProgressCircle/{README.md → v1/README.md} +0 -0
  87. /package/src/ProgressCircle/{props.ts → v1/props.ts} +0 -0
  88. /package/src/ProgressCircle/{styles.ts → v1/styles.ts} +0 -0
  89. /package/src/ProgressCircle/{theme.ts → v1/theme.ts} +0 -0
  90. /package/types/ProgressBar/{index.d.ts → v1/index.d.ts} +0 -0
  91. /package/types/ProgressBar/{props.d.ts → v1/props.d.ts} +0 -0
  92. /package/types/ProgressBar/{styles.d.ts → v1/styles.d.ts} +0 -0
  93. /package/types/ProgressBar/{theme.d.ts → v1/theme.d.ts} +0 -0
  94. /package/types/ProgressCircle/{index.d.ts → v1/index.d.ts} +0 -0
  95. /package/types/ProgressCircle/{props.d.ts → v1/props.d.ts} +0 -0
  96. /package/types/ProgressCircle/{styles.d.ts → v1/styles.d.ts} +0 -0
  97. /package/types/ProgressCircle/{theme.d.ts → v1/theme.d.ts} +0 -0
@@ -0,0 +1,221 @@
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 { NewComponentTypes, SharedTokens } from '@instructure/ui-themes'
26
+ import type { ProgressBarProps, ProgressBarStyle } from './props'
27
+
28
+ /**
29
+ * ---
30
+ * private: true
31
+ * ---
32
+ * Generates the style object from the theme and provided additional information
33
+ * @param {Object} componentTheme The theme variable object.
34
+ * @param {Object} props the props of the component, the style is applied to
35
+ * @param {Object} sharedTokens Shared token object that stores common values for the theme.
36
+ * @return {Object} The final style object, which will be used in the component
37
+ */
38
+ const generateStyle = (
39
+ componentTheme: NewComponentTypes['ProgressBar'],
40
+ props: ProgressBarProps,
41
+ _sharedTokens: SharedTokens
42
+ ): ProgressBarStyle => {
43
+ const {
44
+ valueNow = 0,
45
+ valueMax = 100,
46
+ size,
47
+ color,
48
+ meterColor,
49
+ shouldAnimate
50
+ } = props
51
+
52
+ const meterColorClassName =
53
+ typeof meterColor === 'function'
54
+ ? meterColor({ valueNow, valueMax })
55
+ : meterColor
56
+
57
+ const currentValue =
58
+ valueNow > valueMax ? valueMax : valueNow < 0 ? 0 : valueNow
59
+
60
+ const currentValuePercent = `${(currentValue / valueMax) * 100}%`
61
+
62
+ const sizeVariants = {
63
+ 'x-small': {
64
+ track: { height: componentTheme.xSmallHeight },
65
+ value: { fontSize: componentTheme.xSmallValueFontSize }
66
+ },
67
+ small: {
68
+ track: { height: componentTheme.smallHeight }, // product design wants = 18px
69
+ value: { fontSize: componentTheme.smallValueFontSize }
70
+ },
71
+ medium: {
72
+ track: { height: componentTheme.mediumHeight },
73
+ value: { fontSize: componentTheme.mediumValueFontSize }
74
+ },
75
+ large: {
76
+ track: { height: componentTheme.largeHeight },
77
+ value: { fontSize: componentTheme.largeValueFontSize }
78
+ }
79
+ }
80
+
81
+ const colorVariants = {
82
+ primary: {
83
+ trackLayout: {
84
+ background: componentTheme.trackColor,
85
+ borderWidth: componentTheme.trackBottomBorderWidth,
86
+ borderStyle: 'solid',
87
+ borderColor: componentTheme.borderColor
88
+ },
89
+ trackBorder: {
90
+ borderBottomColor: componentTheme.trackBottomBorderColor
91
+ },
92
+ value: {
93
+ color: componentTheme.textColor
94
+ },
95
+ htmlProgress: {
96
+ borderColor: componentTheme.borderColor
97
+ }
98
+ },
99
+ 'primary-inverse': {
100
+ trackLayout: {
101
+ background: componentTheme.trackColorInverse,
102
+ borderWidth: componentTheme.trackBottomBorderWidth,
103
+ borderStyle: 'solid',
104
+ borderColor: componentTheme.borderColorInverse
105
+ },
106
+ trackBorder: {
107
+ borderBottomColor: componentTheme.trackBottomBorderColorInverse
108
+ },
109
+ value: {
110
+ color: componentTheme.textColorInverse
111
+ },
112
+ htmlProgress: {
113
+ borderColor: componentTheme.borderColorInverse
114
+ }
115
+ }
116
+ }
117
+
118
+ const trackBackgroundVariants = {
119
+ primary: {
120
+ brand: { background: componentTheme.meterColorBrand },
121
+ info: { background: componentTheme.meterColorInfo },
122
+ success: { background: componentTheme.meterColorSuccess },
123
+ danger: { background: componentTheme.meterColorDanger },
124
+ warning: { background: componentTheme.meterColorWarning },
125
+ alert: { background: componentTheme.meterColorAlert }
126
+ },
127
+ 'primary-inverse': {
128
+ brand: { background: componentTheme.meterColorBrandInverse },
129
+ info: { background: componentTheme.meterColorInfoInverse },
130
+ success: { background: componentTheme.meterColorSuccessInverse },
131
+ danger: { background: componentTheme.meterColorDangerInverse },
132
+ warning: { background: componentTheme.meterColorWarningInverse },
133
+ alert: { background: componentTheme.meterColorAlertInverse }
134
+ }
135
+ }
136
+
137
+ return {
138
+ progressBar: {
139
+ label: 'progressBar',
140
+ display: 'flex',
141
+ alignItems: 'center',
142
+ fontFamily: componentTheme.fontFamily,
143
+ fontWeight: componentTheme.fontWeight,
144
+ lineHeight: componentTheme.lineHeight,
145
+ fontSize: componentTheme.fontSize,
146
+ borderRadius: componentTheme.borderRadius
147
+ },
148
+
149
+ trackLayout: {
150
+ label: 'progressBar__trackLayout',
151
+ position: 'relative',
152
+ flex: 1,
153
+ borderRadius: 'inherit',
154
+
155
+ ...colorVariants[color!].trackLayout
156
+ },
157
+
158
+ track: {
159
+ label: 'progressBar__track',
160
+ display: 'block',
161
+ boxSizing: 'border-box',
162
+ width: '100%',
163
+ borderBottomWidth: componentTheme.trackBottomBorderWidth,
164
+ borderBottomStyle: 'solid',
165
+ background: 'transparent',
166
+ // Inner border-radius must be smaller than outer to avoid geometric gaps with nested rounded borders
167
+ // Formula: inner radius = outer radius - border width
168
+ borderTopLeftRadius: `calc(${componentTheme.borderRadius} - ${componentTheme.trackBottomBorderWidth})`,
169
+ borderBottomLeftRadius: `calc(${componentTheme.borderRadius} - ${componentTheme.trackBottomBorderWidth})`,
170
+ // Negative margin pulls track down so trackLayout's full border overlaps and hides track's bottom border.
171
+ // When trackLayout border is visible (light/dark themes), it creates a clean full border appearance.
172
+ // When trackLayout border is transparent (legacy Canvas), track's bottom border remains visible.
173
+ marginBottom: `calc(-1 * ${componentTheme.trackBottomBorderWidth})`,
174
+
175
+ ...sizeVariants[size!].track,
176
+ ...colorVariants[color!].trackBorder
177
+ },
178
+
179
+ trackValue: {
180
+ label: 'progressBar__trackValue',
181
+ display: 'block',
182
+ boxSizing: 'border-box',
183
+ height: '100%',
184
+ width: currentValuePercent,
185
+ maxWidth: '100%',
186
+ borderRadius: 'inherit',
187
+
188
+ ...(shouldAnimate && { transition: 'all 0.5s' }),
189
+ ...(meterColorClassName &&
190
+ trackBackgroundVariants[color!][meterColorClassName])
191
+ },
192
+
193
+ value: {
194
+ label: 'progressBar__value',
195
+ lineHeight: 1,
196
+ boxSizing: 'border-box',
197
+ paddingInlineStart: componentTheme.valuePadding,
198
+ flex: '0 0 5.625rem',
199
+
200
+ ...sizeVariants[size!].value,
201
+ ...colorVariants[color!].value
202
+ },
203
+
204
+ htmlProgress: {
205
+ label: 'progressBar__htmlProgress',
206
+ display: 'block',
207
+ position: 'absolute',
208
+ top: '0',
209
+ left: '0',
210
+ width: '100%',
211
+ height: '100%',
212
+ boxSizing: 'border-box',
213
+ zIndex: -1,
214
+ opacity: 0,
215
+
216
+ ...colorVariants[color!].htmlProgress
217
+ }
218
+ }
219
+ }
220
+
221
+ export default generateStyle
@@ -25,10 +25,10 @@
25
25
  import { Component } from 'react'
26
26
 
27
27
  import { ScreenReaderContent } from '@instructure/ui-a11y-content'
28
- import { View } from '@instructure/ui-view'
28
+ import { View } from '@instructure/ui-view/v11_6'
29
29
  import { callRenderProp, passthroughProps } from '@instructure/ui-react-utils'
30
30
 
31
- import { withStyle } from '@instructure/emotion'
31
+ import { withStyleLegacy as withStyle } from '@instructure/emotion'
32
32
 
33
33
  import generateStyle from './styles'
34
34
  import generateComponentTheme from './theme'
@@ -0,0 +1,219 @@
1
+ ---
2
+ describes: ProgressCircle
3
+ ---
4
+
5
+ `<ProgressCircle />` is a 🍩 styled HTML `<progress />` element, complete
6
+ with the aria attributes required to support screen readers.
7
+
8
+ ### `size`, `shouldAnimateOnMount`, `animationDelay`
9
+
10
+ ```js
11
+ ---
12
+ type: example
13
+ ---
14
+ <div>
15
+ <ProgressCircle
16
+ size="x-small"
17
+ screenReaderLabel="Loading completion"
18
+ valueNow={40}
19
+ valueMax={60}
20
+ margin="0 small 0 0"
21
+ shouldAnimateOnMount
22
+ />
23
+ <ProgressCircle
24
+ size="small"
25
+ screenReaderLabel="Loading completion"
26
+ valueNow={40}
27
+ valueMax={60}
28
+ margin="0 small 0 0"
29
+ shouldAnimateOnMount
30
+ animationDelay={2000}
31
+ />
32
+ <ProgressCircle
33
+ screenReaderLabel="Loading completion"
34
+ valueNow={40}
35
+ valueMax={60}
36
+ margin="0 small 0 0"
37
+ shouldAnimateOnMount
38
+ animationDelay={4000}
39
+ formatScreenReaderValue={function ({ valueNow, valueMax }) {
40
+ return valueNow + ' out of ' + valueMax
41
+ }}
42
+ renderValue={function ({ valueNow, valueMax }) {
43
+ return (
44
+ <span>
45
+ <Text size="large" weight="bold">{valueNow}</Text>
46
+ <br />
47
+ <Text size="small">/&nbsp;</Text>
48
+ <Text size="small">{valueMax}</Text>
49
+ </span>
50
+ )
51
+ }}
52
+ />
53
+ <ProgressCircle
54
+ size="large"
55
+ screenReaderLabel="Loading completion"
56
+ valueNow={40}
57
+ valueMax={60}
58
+ shouldAnimateOnMount
59
+ animationDelay={6000}
60
+ formatScreenReaderValue={function ({ valueNow, valueMax }) {
61
+ const passing = valueNow > (valueMax / 2) ? 'pass' : 'fail'
62
+ return `${valueNow} of ${valueMax}: ${passing}`
63
+ }}
64
+ renderValue={function ({ valueNow, valueMax }) {
65
+ if (valueNow > (valueMax / 2)) {
66
+ return (
67
+ <Text weight="bold" size="large">
68
+ PASS
69
+ </Text>
70
+ )
71
+ } else {
72
+ return (
73
+ <Text weight="bold" size="large">
74
+ FAIL
75
+ </Text>
76
+ )
77
+ }
78
+ }}
79
+ />
80
+ </div>
81
+ ```
82
+
83
+ ### `color`
84
+
85
+ `<ProgressCircle />`'s `color` property controls the overall color scheme of the
86
+ component. Set it to `primary-inverse` when the component is used on dark backgrounds.
87
+
88
+ ```js
89
+ ---
90
+ type: example
91
+
92
+ ---
93
+ <View background="primary-inverse" as="div">
94
+ <ProgressCircle
95
+ screenReaderLabel="Loading completion"
96
+ color="primary-inverse"
97
+ valueNow={50}
98
+ valueMax={60}
99
+ />
100
+ </View>
101
+ ```
102
+
103
+ ### `meterColor`
104
+
105
+ The color of the progress meter is set separately through the `meterColor` property.
106
+
107
+ ```js
108
+ ---
109
+ type: example
110
+ ---
111
+ <div>
112
+ <ProgressCircle
113
+ screenReaderLabel="Loading completion"
114
+ meterColor="info"
115
+ valueNow={40}
116
+ valueMax={60}
117
+ margin="0 0 small"
118
+ />
119
+ <ProgressCircle
120
+ screenReaderLabel="Loading completion"
121
+ meterColor="success"
122
+ valueNow={40}
123
+ valueMax={60}
124
+ margin="0 0 small"
125
+ />
126
+ <ProgressCircle
127
+ screenReaderLabel="Loading completion"
128
+ meterColor="alert"
129
+ valueNow={40}
130
+ valueMax={60}
131
+ margin="0 0 small"
132
+ />
133
+ <ProgressCircle
134
+ screenReaderLabel="Loading completion"
135
+ meterColor="warning"
136
+ valueNow={40}
137
+ valueMax={60}
138
+ margin="0 0 small"
139
+ />
140
+ <ProgressCircle
141
+ screenReaderLabel="Loading completion"
142
+ meterColor="danger"
143
+ valueNow={40}
144
+ valueMax={60}
145
+ margin="0 0 small"
146
+ />
147
+ </div>
148
+ ```
149
+
150
+ `meterColor` can also be passed in as a function that takes `valueNow / valueMax`
151
+ as parameters. This allows you to adjust the color of the meter based on the
152
+ current value.
153
+
154
+ ```js
155
+ ---
156
+ type: example
157
+ ---
158
+ <ProgressCircle
159
+ screenReaderLabel="Loading completion"
160
+ meterColor={({ valueNow, valueMax }) => {
161
+ if (valueNow < 20) {
162
+ return 'danger'
163
+ } else if (valueNow / valueMax >= 1) {
164
+ return 'success'
165
+ } else {
166
+ return 'info'
167
+ }
168
+ }}
169
+ valueNow={10}
170
+ valueMax={60}
171
+ />
172
+ ```
173
+
174
+ > `<ProgressCircle />` defaults to setting the meter color to `success` when
175
+ > complete.
176
+
177
+ ### `renderValue` / `formatScreenReaderValue`
178
+
179
+ Via the `renderValue` prop, developers can use `valueMax` and `valueNow` props to format the
180
+ value that `<ProgressCircle />` displays.
181
+
182
+ > `renderValue` will not be spoken by screen readers. Any essential information
183
+ > in `renderValue` must also be conveyed via `formatScreenReaderValue` for screen reader users.
184
+
185
+ ```js
186
+ ---
187
+ type: example
188
+ ---
189
+ <ProgressCircle
190
+ screenReaderLabel="Percent complete"
191
+ formatScreenReaderValue={({ valueNow, valueMax }) => {
192
+ return Math.round((valueNow / valueMax * 100)) + ' percent'
193
+ }}
194
+ renderValue={({ valueNow, valueMax }) => {
195
+ const percent = Math.round(valueNow / valueMax * 100)
196
+ return (
197
+ <span>
198
+ <Text size="large" weight="bold" color="primary">
199
+ {percent > 100 ? 100 : percent}
200
+ </Text>
201
+ <Text size="small" weight="bold" color="secondary">%</Text>
202
+ </span>
203
+ )
204
+ }}
205
+ valueMax={88}
206
+ valueNow={33}
207
+ />
208
+ ```
209
+
210
+ ```js
211
+ ---
212
+ type: embed
213
+ ---
214
+ <Guidelines>
215
+ <Figure recommendation="a11y" title="Accessibility">
216
+ <Figure.Item>If the progress bar conveys more information than just an approximate progress of a task (for example "5 or 23 items downloaded") then show this information as text too.</Figure.Item>
217
+ </Figure>
218
+ </Guidelines>
219
+ ```
@@ -0,0 +1,196 @@
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 { Component } from 'react'
26
+
27
+ import { ScreenReaderContent } from '@instructure/ui-a11y-content'
28
+ import { View } from '@instructure/ui-view/latest'
29
+ import { callRenderProp, passthroughProps } from '@instructure/ui-react-utils'
30
+
31
+ import { withStyle } from '@instructure/emotion'
32
+
33
+ import generateStyle from './styles'
34
+ import type { ProgressCircleProps, ProgressCircleState, Values } from './props'
35
+ import { allowedProps } from './props'
36
+
37
+ /**
38
+ ---
39
+ category: components
40
+ ---
41
+ **/
42
+ @withStyle(generateStyle)
43
+ class ProgressCircle extends Component<
44
+ ProgressCircleProps,
45
+ ProgressCircleState
46
+ > {
47
+ static readonly componentId = 'ProgressCircle'
48
+
49
+ static allowedProps = allowedProps
50
+
51
+ static defaultProps = {
52
+ formatScreenReaderValue: ({ valueNow, valueMax }: Values) =>
53
+ `${valueNow} / ${valueMax}`,
54
+ size: 'medium',
55
+ valueMax: 100,
56
+ valueNow: 0,
57
+ as: 'div',
58
+ color: 'primary',
59
+ shouldAnimateOnMount: false,
60
+
61
+ // default to showing `success` color on completion
62
+ meterColor: ({ valueNow, valueMax }: Values) =>
63
+ valueNow / valueMax >= 1 ? 'success' : 'brand'
64
+ }
65
+
66
+ _timeouts: Array<ReturnType<typeof setTimeout>> = []
67
+ ref: Element | null = null
68
+
69
+ constructor(props: ProgressCircleProps) {
70
+ super(props)
71
+
72
+ this.state = {
73
+ shouldAnimateOnMount: props.shouldAnimateOnMount!
74
+ }
75
+ }
76
+
77
+ handleRef = (el: Element | null) => {
78
+ const { elementRef } = this.props
79
+
80
+ this.ref = el
81
+
82
+ if (typeof elementRef === 'function') {
83
+ elementRef(el)
84
+ }
85
+ }
86
+
87
+ get makeStylesVariables() {
88
+ return { shouldAnimateOnMount: this.state.shouldAnimateOnMount }
89
+ }
90
+
91
+ componentDidMount() {
92
+ if (this.state.shouldAnimateOnMount) {
93
+ this._timeouts.push(
94
+ setTimeout(() => {
95
+ this.setState({
96
+ shouldAnimateOnMount: false
97
+ })
98
+ }, this.props.animationDelay || 500)
99
+ )
100
+ }
101
+
102
+ this.props.makeStyles?.(this.makeStylesVariables)
103
+ }
104
+
105
+ componentDidUpdate() {
106
+ this.props.makeStyles?.(this.makeStylesVariables)
107
+ }
108
+
109
+ componentWillUnmount() {
110
+ this._timeouts.forEach((timeout) => clearTimeout(timeout))
111
+ }
112
+
113
+ render() {
114
+ const {
115
+ color,
116
+ renderValue,
117
+ formatScreenReaderValue,
118
+ meterColor,
119
+ valueNow,
120
+ valueMax,
121
+ screenReaderLabel,
122
+ size,
123
+ styles,
124
+ ...props
125
+ } = this.props
126
+
127
+ const valueText =
128
+ typeof formatScreenReaderValue === 'function'
129
+ ? formatScreenReaderValue({ valueNow: valueNow!, valueMax: valueMax! })
130
+ : undefined
131
+ // consolidating the label and aria-valuetext to put in aria-label because
132
+ // NVDA does not read aria-valuetext: https://github.com/nvaccess/nvda/issues/913
133
+ // But leaving aria-valuetext because JAWS ignores aria-label
134
+ const labelAndValueText = `${screenReaderLabel} ${valueText}`
135
+
136
+ const value = callRenderProp(renderValue, {
137
+ valueNow: valueNow!,
138
+ valueMax: valueMax!
139
+ })
140
+
141
+ const style = {
142
+ strokeDashoffset: `${styles?.dashOffset}em`
143
+ }
144
+
145
+ return (
146
+ <View
147
+ {...passthroughProps(props)}
148
+ as={this.props.as}
149
+ elementRef={this.handleRef}
150
+ css={styles?.progressCircle}
151
+ margin={this.props.margin}
152
+ data-cid="ProgressCircle"
153
+ >
154
+ <ScreenReaderContent>
155
+ <progress
156
+ max={valueMax}
157
+ value={valueNow}
158
+ aria-valuetext={labelAndValueText}
159
+ />
160
+ </ScreenReaderContent>
161
+ {value && (
162
+ <span css={styles?.center} aria-hidden="true">
163
+ <span css={styles?.value}>{value}</span>
164
+ </span>
165
+ )}
166
+ <svg css={styles?.circle} role="presentation" focusable="false">
167
+ <circle
168
+ css={styles?.track}
169
+ role="presentation"
170
+ cx="50%"
171
+ cy="50%"
172
+ r={styles?.radii?.radius}
173
+ />
174
+ <circle
175
+ css={styles?.border}
176
+ role="presentation"
177
+ cx="50%"
178
+ cy="50%"
179
+ r={styles?.radii?.borderOffsetRadius}
180
+ />
181
+ <circle
182
+ css={styles?.meter}
183
+ role="presentation"
184
+ style={style}
185
+ cx="50%"
186
+ cy="50%"
187
+ r={styles?.radii?.radius}
188
+ />
189
+ </svg>
190
+ </View>
191
+ )
192
+ }
193
+ }
194
+
195
+ export default ProgressCircle
196
+ export { ProgressCircle }