@instructure/ui-checkbox 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 (158) hide show
  1. package/CHANGELOG.md +44 -297
  2. package/es/Checkbox/{CheckboxFacade → v1/CheckboxFacade}/index.js +1 -1
  3. package/es/Checkbox/{ToggleFacade → v1/ToggleFacade}/index.js +1 -1
  4. package/es/Checkbox/{index.js → v1/index.js} +3 -3
  5. package/es/Checkbox/v2/CheckboxFacade/index.js +104 -0
  6. package/es/Checkbox/v2/CheckboxFacade/props.js +26 -0
  7. package/es/Checkbox/v2/CheckboxFacade/styles.js +177 -0
  8. package/es/Checkbox/v2/ToggleFacade/index.js +116 -0
  9. package/es/Checkbox/v2/ToggleFacade/props.js +26 -0
  10. package/es/Checkbox/v2/ToggleFacade/styles.js +232 -0
  11. package/es/Checkbox/v2/index.js +289 -0
  12. package/es/Checkbox/v2/props.js +26 -0
  13. package/es/Checkbox/v2/styles.js +102 -0
  14. package/es/CheckboxGroup/{index.js → v1/index.js} +2 -2
  15. package/es/CheckboxGroup/v2/index.js +125 -0
  16. package/es/CheckboxGroup/v2/props.js +26 -0
  17. package/es/{index.js → exports/a.js} +2 -2
  18. package/es/exports/b.js +25 -0
  19. package/lib/Checkbox/{CheckboxFacade → v1/CheckboxFacade}/index.js +2 -2
  20. package/lib/Checkbox/{ToggleFacade → v1/ToggleFacade}/index.js +3 -3
  21. package/lib/Checkbox/{index.js → v1/index.js} +5 -5
  22. package/lib/Checkbox/v2/CheckboxFacade/index.js +109 -0
  23. package/lib/Checkbox/v2/CheckboxFacade/props.js +31 -0
  24. package/lib/Checkbox/v2/CheckboxFacade/styles.js +183 -0
  25. package/lib/Checkbox/v2/ToggleFacade/index.js +121 -0
  26. package/lib/Checkbox/v2/ToggleFacade/props.js +31 -0
  27. package/lib/Checkbox/v2/ToggleFacade/styles.js +238 -0
  28. package/lib/Checkbox/v2/index.js +308 -0
  29. package/lib/Checkbox/v2/props.js +31 -0
  30. package/lib/Checkbox/v2/styles.js +108 -0
  31. package/lib/CheckboxGroup/{index.js → v1/index.js} +5 -5
  32. package/lib/CheckboxGroup/v2/index.js +133 -0
  33. package/lib/CheckboxGroup/v2/props.js +31 -0
  34. package/lib/{index.js → exports/a.js} +6 -6
  35. package/lib/exports/b.js +31 -0
  36. package/package.json +45 -23
  37. package/src/Checkbox/{CheckboxFacade → v1/CheckboxFacade}/index.tsx +1 -1
  38. package/src/Checkbox/{CheckboxFacade → v1/CheckboxFacade}/styles.ts +3 -1
  39. package/src/Checkbox/{ToggleFacade → v1/ToggleFacade}/index.tsx +1 -1
  40. package/src/Checkbox/{index.tsx → v1/index.tsx} +3 -3
  41. package/src/Checkbox/{props.ts → v1/props.ts} +1 -1
  42. package/src/Checkbox/{styles.ts → v1/styles.ts} +1 -1
  43. package/src/Checkbox/{theme.ts → v1/theme.ts} +1 -1
  44. package/src/Checkbox/v2/CheckboxFacade/index.tsx +113 -0
  45. package/src/Checkbox/v2/CheckboxFacade/props.ts +67 -0
  46. package/src/Checkbox/v2/CheckboxFacade/styles.ts +209 -0
  47. package/src/Checkbox/v2/README.md +193 -0
  48. package/src/Checkbox/v2/ToggleFacade/index.tsx +124 -0
  49. package/src/Checkbox/v2/ToggleFacade/props.ts +67 -0
  50. package/src/Checkbox/v2/ToggleFacade/styles.ts +301 -0
  51. package/src/Checkbox/v2/index.tsx +376 -0
  52. package/src/Checkbox/v2/props.ts +132 -0
  53. package/src/Checkbox/v2/styles.ts +118 -0
  54. package/src/CheckboxGroup/{index.tsx → v1/index.tsx} +2 -2
  55. package/src/CheckboxGroup/{props.ts → v1/props.ts} +3 -3
  56. package/src/CheckboxGroup/v2/README.md +111 -0
  57. package/src/CheckboxGroup/v2/index.tsx +157 -0
  58. package/src/CheckboxGroup/v2/props.ts +77 -0
  59. package/src/{index.ts → exports/a.ts} +6 -6
  60. package/src/exports/b.ts +30 -0
  61. package/tsconfig.build.tsbuildinfo +1 -1
  62. package/types/Checkbox/v1/CheckboxFacade/index.d.ts.map +1 -0
  63. package/types/Checkbox/v1/CheckboxFacade/props.d.ts.map +1 -0
  64. package/types/Checkbox/v1/CheckboxFacade/styles.d.ts.map +1 -0
  65. package/types/Checkbox/v1/CheckboxFacade/theme.d.ts.map +1 -0
  66. package/types/Checkbox/v1/ToggleFacade/index.d.ts.map +1 -0
  67. package/types/Checkbox/v1/ToggleFacade/props.d.ts.map +1 -0
  68. package/types/Checkbox/v1/ToggleFacade/styles.d.ts.map +1 -0
  69. package/types/Checkbox/v1/ToggleFacade/theme.d.ts.map +1 -0
  70. package/types/Checkbox/{index.d.ts → v1/index.d.ts} +1 -1
  71. package/types/Checkbox/v1/index.d.ts.map +1 -0
  72. package/types/Checkbox/{props.d.ts → v1/props.d.ts} +1 -1
  73. package/types/Checkbox/v1/props.d.ts.map +1 -0
  74. package/types/Checkbox/v1/styles.d.ts.map +1 -0
  75. package/types/Checkbox/v1/theme.d.ts.map +1 -0
  76. package/types/Checkbox/v2/CheckboxFacade/index.d.ts +39 -0
  77. package/types/Checkbox/v2/CheckboxFacade/index.d.ts.map +1 -0
  78. package/types/Checkbox/v2/CheckboxFacade/props.d.ts +27 -0
  79. package/types/Checkbox/v2/CheckboxFacade/props.d.ts.map +1 -0
  80. package/types/Checkbox/v2/CheckboxFacade/styles.d.ts +16 -0
  81. package/types/Checkbox/v2/CheckboxFacade/styles.d.ts.map +1 -0
  82. package/types/Checkbox/v2/ToggleFacade/index.d.ts +40 -0
  83. package/types/Checkbox/v2/ToggleFacade/index.d.ts.map +1 -0
  84. package/types/Checkbox/v2/ToggleFacade/props.d.ts +25 -0
  85. package/types/Checkbox/v2/ToggleFacade/props.d.ts.map +1 -0
  86. package/types/Checkbox/v2/ToggleFacade/styles.d.ts +16 -0
  87. package/types/Checkbox/v2/ToggleFacade/styles.d.ts.map +1 -0
  88. package/types/Checkbox/v2/index.d.ts +71 -0
  89. package/types/Checkbox/v2/index.d.ts.map +1 -0
  90. package/types/Checkbox/v2/props.d.ts +64 -0
  91. package/types/Checkbox/v2/props.d.ts.map +1 -0
  92. package/types/Checkbox/v2/styles.d.ts +16 -0
  93. package/types/Checkbox/v2/styles.d.ts.map +1 -0
  94. package/types/CheckboxGroup/{index.d.ts → v1/index.d.ts} +1 -1
  95. package/types/CheckboxGroup/v1/index.d.ts.map +1 -0
  96. package/types/CheckboxGroup/{props.d.ts → v1/props.d.ts} +3 -3
  97. package/types/CheckboxGroup/v1/props.d.ts.map +1 -0
  98. package/types/CheckboxGroup/v2/index.d.ts +42 -0
  99. package/types/CheckboxGroup/v2/index.d.ts.map +1 -0
  100. package/types/CheckboxGroup/v2/props.d.ts +30 -0
  101. package/types/CheckboxGroup/v2/props.d.ts.map +1 -0
  102. package/types/exports/a.d.ts +7 -0
  103. package/types/exports/a.d.ts.map +1 -0
  104. package/types/exports/b.d.ts +7 -0
  105. package/types/exports/b.d.ts.map +1 -0
  106. package/types/Checkbox/CheckboxFacade/index.d.ts.map +0 -1
  107. package/types/Checkbox/CheckboxFacade/props.d.ts.map +0 -1
  108. package/types/Checkbox/CheckboxFacade/styles.d.ts.map +0 -1
  109. package/types/Checkbox/CheckboxFacade/theme.d.ts.map +0 -1
  110. package/types/Checkbox/ToggleFacade/index.d.ts.map +0 -1
  111. package/types/Checkbox/ToggleFacade/props.d.ts.map +0 -1
  112. package/types/Checkbox/ToggleFacade/styles.d.ts.map +0 -1
  113. package/types/Checkbox/ToggleFacade/theme.d.ts.map +0 -1
  114. package/types/Checkbox/index.d.ts.map +0 -1
  115. package/types/Checkbox/props.d.ts.map +0 -1
  116. package/types/Checkbox/styles.d.ts.map +0 -1
  117. package/types/Checkbox/theme.d.ts.map +0 -1
  118. package/types/CheckboxGroup/index.d.ts.map +0 -1
  119. package/types/CheckboxGroup/props.d.ts.map +0 -1
  120. package/types/index.d.ts +0 -7
  121. package/types/index.d.ts.map +0 -1
  122. /package/es/Checkbox/{CheckboxFacade → v1/CheckboxFacade}/props.js +0 -0
  123. /package/es/Checkbox/{CheckboxFacade → v1/CheckboxFacade}/styles.js +0 -0
  124. /package/es/Checkbox/{CheckboxFacade → v1/CheckboxFacade}/theme.js +0 -0
  125. /package/es/Checkbox/{ToggleFacade → v1/ToggleFacade}/props.js +0 -0
  126. /package/es/Checkbox/{ToggleFacade → v1/ToggleFacade}/styles.js +0 -0
  127. /package/es/Checkbox/{ToggleFacade → v1/ToggleFacade}/theme.js +0 -0
  128. /package/es/Checkbox/{props.js → v1/props.js} +0 -0
  129. /package/es/Checkbox/{styles.js → v1/styles.js} +0 -0
  130. /package/es/Checkbox/{theme.js → v1/theme.js} +0 -0
  131. /package/es/CheckboxGroup/{props.js → v1/props.js} +0 -0
  132. /package/lib/Checkbox/{CheckboxFacade → v1/CheckboxFacade}/props.js +0 -0
  133. /package/lib/Checkbox/{CheckboxFacade → v1/CheckboxFacade}/styles.js +0 -0
  134. /package/lib/Checkbox/{CheckboxFacade → v1/CheckboxFacade}/theme.js +0 -0
  135. /package/lib/Checkbox/{ToggleFacade → v1/ToggleFacade}/props.js +0 -0
  136. /package/lib/Checkbox/{ToggleFacade → v1/ToggleFacade}/styles.js +0 -0
  137. /package/lib/Checkbox/{ToggleFacade → v1/ToggleFacade}/theme.js +0 -0
  138. /package/lib/Checkbox/{props.js → v1/props.js} +0 -0
  139. /package/lib/Checkbox/{styles.js → v1/styles.js} +0 -0
  140. /package/lib/Checkbox/{theme.js → v1/theme.js} +0 -0
  141. /package/lib/CheckboxGroup/{props.js → v1/props.js} +0 -0
  142. /package/src/Checkbox/{CheckboxFacade → v1/CheckboxFacade}/props.ts +0 -0
  143. /package/src/Checkbox/{CheckboxFacade → v1/CheckboxFacade}/theme.ts +0 -0
  144. /package/src/Checkbox/{README.md → v1/README.md} +0 -0
  145. /package/src/Checkbox/{ToggleFacade → v1/ToggleFacade}/props.ts +0 -0
  146. /package/src/Checkbox/{ToggleFacade → v1/ToggleFacade}/styles.ts +0 -0
  147. /package/src/Checkbox/{ToggleFacade → v1/ToggleFacade}/theme.ts +0 -0
  148. /package/src/CheckboxGroup/{README.md → v1/README.md} +0 -0
  149. /package/types/Checkbox/{CheckboxFacade → v1/CheckboxFacade}/index.d.ts +0 -0
  150. /package/types/Checkbox/{CheckboxFacade → v1/CheckboxFacade}/props.d.ts +0 -0
  151. /package/types/Checkbox/{CheckboxFacade → v1/CheckboxFacade}/styles.d.ts +0 -0
  152. /package/types/Checkbox/{CheckboxFacade → v1/CheckboxFacade}/theme.d.ts +0 -0
  153. /package/types/Checkbox/{ToggleFacade → v1/ToggleFacade}/index.d.ts +0 -0
  154. /package/types/Checkbox/{ToggleFacade → v1/ToggleFacade}/props.d.ts +0 -0
  155. /package/types/Checkbox/{ToggleFacade → v1/ToggleFacade}/styles.d.ts +0 -0
  156. /package/types/Checkbox/{ToggleFacade → v1/ToggleFacade}/theme.d.ts +0 -0
  157. /package/types/Checkbox/{styles.d.ts → v1/styles.d.ts} +0 -0
  158. /package/types/Checkbox/{theme.d.ts → v1/theme.d.ts} +0 -0
@@ -0,0 +1,301 @@
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 { boxShadowObjectsToCSSString } from '@instructure/ui-themes'
27
+ import { calcFocusOutlineStyles } from '@instructure/emotion'
28
+ import type { ToggleFacadeProps, ToggleFacadeStyle } from './props'
29
+
30
+ /**
31
+ * ---
32
+ * private: true
33
+ * ---
34
+ * Generates the style object from the theme and provided additional information
35
+ * @param {Object} componentTheme The theme variable object.
36
+ * @param {Object} props the props of the component, the style is applied to
37
+ * @param {Object} sharedTokens Shared theme token object
38
+ * @param {Object} state the state of the component, the style is applied to
39
+ * @return {Object} The final style object, which will be used in the component
40
+ */
41
+ const generateStyle = (
42
+ componentTheme: NewComponentTypes['Toggle'],
43
+ props: ToggleFacadeProps,
44
+ sharedTokens: SharedTokens
45
+ ): ToggleFacadeStyle => {
46
+ const {
47
+ disabled,
48
+ size,
49
+ checked,
50
+ readOnly,
51
+ focused,
52
+ hovered,
53
+ labelPlacement,
54
+ invalid
55
+ } = props
56
+
57
+ const labelPlacementVariants = {
58
+ start: {
59
+ facade: {
60
+ marginInlineEnd: componentTheme.marginEnd,
61
+ marginInlineStart: componentTheme.marginStart
62
+ },
63
+ label: {
64
+ textAlign: 'end'
65
+ }
66
+ },
67
+ end: {
68
+ facade: {
69
+ marginInlineEnd: componentTheme.marginEnd
70
+ },
71
+ label: {}
72
+ },
73
+ top: {
74
+ facade: {
75
+ marginTop: componentTheme.marginVertical
76
+ },
77
+ label: {
78
+ display: 'block'
79
+ }
80
+ }
81
+ }
82
+ const labelPlacementVariant =
83
+ labelPlacementVariants[
84
+ labelPlacement as keyof typeof labelPlacementVariants
85
+ ] ?? labelPlacementVariants.end
86
+
87
+ const labelSizeVariants = {
88
+ small: {
89
+ fontSize: componentTheme.labelFontSizeSm,
90
+ lineHeight: componentTheme.labelLineHeightSm
91
+ },
92
+ medium: {
93
+ fontSize: componentTheme.labelFontSizeMd,
94
+ lineHeight: componentTheme.labelLineHeightMd
95
+ },
96
+ large: {
97
+ fontSize: componentTheme.labelFontSizeLg,
98
+ lineHeight: componentTheme.labelLineHeightLg
99
+ }
100
+ }
101
+ const sizeVariant =
102
+ labelSizeVariants[size as keyof typeof labelSizeVariants] ??
103
+ labelSizeVariants.medium
104
+
105
+ const getIconBorderColor = () => {
106
+ if (disabled) {
107
+ return checked
108
+ ? componentTheme.checkedIconBorderDisabledColor
109
+ : componentTheme.uncheckedIconBorderDisabledColor
110
+ }
111
+
112
+ if (readOnly) {
113
+ return checked
114
+ ? componentTheme.checkedIconBorderReadonlyColor
115
+ : componentTheme.uncheckedIconBorderReadonlyColor
116
+ }
117
+
118
+ if (invalid && !checked) {
119
+ return hovered
120
+ ? componentTheme.uncheckedIconBorderHoverColor
121
+ : componentTheme.uncheckedIconErrorBorderColor
122
+ }
123
+
124
+ if (checked) {
125
+ return hovered
126
+ ? componentTheme.checkedIconBorderHoverColor
127
+ : componentTheme.checkedIconBorderColor
128
+ }
129
+
130
+ // DEFAULT (unchecked) state
131
+ return hovered
132
+ ? componentTheme.uncheckedIconBorderHoverColor
133
+ : componentTheme.uncheckedIconBorderColor
134
+ }
135
+
136
+ const getFacadeStyles = () => {
137
+ const baseStyles = {
138
+ display: 'inline-block',
139
+ userSelect: 'none',
140
+ position: 'relative',
141
+ borderRadius: componentTheme.borderRadius,
142
+ verticalAlign: 'middle',
143
+ height: componentTheme.toggleSize,
144
+ width: componentTheme.toggleWidth,
145
+ ...labelPlacementVariant.facade
146
+ }
147
+
148
+ if (disabled) {
149
+ return {
150
+ ...baseStyles,
151
+ background: checked
152
+ ? componentTheme.checkedBackgroundDisabledColor
153
+ : componentTheme.backgroundDisabledColor,
154
+ boxShadow: `inset 0 0 0 ${componentTheme.borderWidth} ${
155
+ checked
156
+ ? componentTheme.checkedBorderDisabledColor
157
+ : componentTheme.borderDisabledColor
158
+ }`,
159
+ opacity: componentTheme.disabledOpacity
160
+ }
161
+ }
162
+
163
+ if (readOnly) {
164
+ return {
165
+ ...baseStyles,
166
+ background: checked
167
+ ? componentTheme.checkedBackgroundReadonlyColor
168
+ : componentTheme.backgroundReadonlyColor,
169
+ boxShadow: `inset 0 0 0 ${componentTheme.borderWidth} ${
170
+ checked
171
+ ? componentTheme.checkedBorderReadonlyColor
172
+ : componentTheme.borderReadonlyColor
173
+ }`,
174
+ pointerEvents: 'none'
175
+ }
176
+ }
177
+
178
+ if (invalid) {
179
+ return {
180
+ ...baseStyles,
181
+ ...(checked && {
182
+ background: hovered
183
+ ? componentTheme.checkedBackgroundHoverColor
184
+ : componentTheme.checkedBackgroundColor,
185
+ boxShadow: `inset 0 0 0 ${componentTheme.borderWidth} ${componentTheme.errorBorderColor}`
186
+ }),
187
+ ...(!checked && {
188
+ background: hovered
189
+ ? componentTheme.errorBackgroundHoverColor
190
+ : componentTheme.errorBackgroundColor,
191
+ boxShadow: `inset 0 0 0 ${componentTheme.borderWidth} ${componentTheme.errorBorderColor}`
192
+ })
193
+ }
194
+ }
195
+
196
+ if (checked) {
197
+ return {
198
+ ...baseStyles,
199
+ background: hovered
200
+ ? componentTheme.checkedBackgroundHoverColor
201
+ : componentTheme.checkedBackgroundColor,
202
+ boxShadow: `inset 0 0 0 ${componentTheme.borderWidth} ${
203
+ hovered
204
+ ? componentTheme.checkedBorderHoverColor
205
+ : componentTheme.checkedBorderColor
206
+ }`
207
+ }
208
+ }
209
+
210
+ // DEFAULT (unchecked) state
211
+ return {
212
+ ...baseStyles,
213
+ background: hovered
214
+ ? componentTheme.backgroundHoverColor
215
+ : componentTheme.backgroundColor,
216
+ boxShadow: `inset 0 0 0 ${componentTheme.borderWidth} ${
217
+ hovered ? componentTheme.borderHoverColor : componentTheme.borderColor
218
+ }`
219
+ }
220
+ }
221
+ return {
222
+ toggleFacade: {
223
+ label: 'toggleFacade',
224
+ display: 'flex',
225
+ alignItems: 'center',
226
+ cursor: disabled ? 'not-allowed' : readOnly ? 'default' : 'pointer',
227
+ ...(labelPlacement === 'top' && { display: 'block' })
228
+ },
229
+ facade: {
230
+ label: 'toggleFacade__facade',
231
+ ...getFacadeStyles(),
232
+ ...(sharedTokens?.focusOutline
233
+ ? calcFocusOutlineStyles(sharedTokens.focusOutline, {
234
+ withFocusOutline: focused
235
+ })
236
+ : {})
237
+ },
238
+
239
+ icon: {
240
+ label: 'toggleFacade__icon',
241
+ display: 'block',
242
+ textAlign: 'center',
243
+ position: 'absolute',
244
+ top: '0',
245
+ insetInlineStart: '0',
246
+ insetInlineEnd: 'auto',
247
+ transition: 'all 0.2s',
248
+ transform: 'translate3d(0, 0, 0)',
249
+ height: componentTheme.toggleSize,
250
+ width: componentTheme.toggleSize,
251
+
252
+ ...(checked && {
253
+ transform: `translateX(calc(${componentTheme.toggleWidth} - ${componentTheme.toggleSize}))`,
254
+ '[dir="rtl"] &': {
255
+ transform: `translateX(calc(-1 * (${componentTheme.toggleWidth} - ${componentTheme.toggleSize})))`
256
+ }
257
+ })
258
+ },
259
+
260
+ iconToggle: {
261
+ label: 'toggleFacade__iconToggle',
262
+ position: 'relative',
263
+ height: '100%',
264
+ width: '100%',
265
+ display: 'flex',
266
+ alignItems: 'center',
267
+ justifyContent: 'center',
268
+
269
+ '&::before': {
270
+ content: '""',
271
+ position: 'absolute',
272
+ height: `calc(100% - (${componentTheme.borderWidth} * 6))`,
273
+ width: `calc(100% - (${componentTheme.borderWidth} * 6))`,
274
+ background: componentTheme.toggleBackground,
275
+ boxShadow: boxShadowObjectsToCSSString(componentTheme.toggleShadow),
276
+ borderRadius: '100%',
277
+ border: `${componentTheme.borderWidth} solid ${getIconBorderColor()}`
278
+ },
279
+
280
+ '& [class*="lucideIcon"] svg': {
281
+ position: 'relative',
282
+ zIndex: 1
283
+ }
284
+ },
285
+
286
+ label: {
287
+ label: 'toggleFacade__label',
288
+ flex: 1,
289
+ minWidth: '0.0625rem',
290
+ color: disabled
291
+ ? componentTheme.labelDisabledColor
292
+ : componentTheme.labelColor,
293
+ fontFamily: componentTheme.labelFontFamily,
294
+ fontWeight: componentTheme.labelFontWeight,
295
+ ...sizeVariant,
296
+ ...labelPlacementVariant.label
297
+ }
298
+ }
299
+ }
300
+
301
+ export default generateStyle
@@ -0,0 +1,376 @@
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
+ import keycode from 'keycode'
27
+
28
+ import { FormFieldMessages } from '@instructure/ui-form-field/latest'
29
+ import { createChainedFunction } from '@instructure/ui-utils'
30
+ import { logError as error } from '@instructure/console'
31
+ import { isActiveElement } from '@instructure/ui-dom-utils'
32
+ import { omitProps, withDeterministicId } from '@instructure/ui-react-utils'
33
+ import { View } from '@instructure/ui-view/latest'
34
+
35
+ import { withStyle } from '@instructure/emotion'
36
+
37
+ import { CheckboxFacade } from './CheckboxFacade'
38
+ import { ToggleFacade } from './ToggleFacade'
39
+
40
+ import generateStyle from './styles'
41
+
42
+ import { allowedProps } from './props'
43
+ import type { CheckboxProps, CheckboxState } from './props'
44
+
45
+ import type {
46
+ CheckboxFacadeTheme,
47
+ ToggleFacadeTheme
48
+ } from '@instructure/shared-types'
49
+
50
+ /**
51
+ ---
52
+ category: components
53
+ tags: toggle, switch
54
+ ---
55
+ **/
56
+
57
+ @withDeterministicId()
58
+ @withStyle(generateStyle)
59
+ class Checkbox extends Component<CheckboxProps, CheckboxState> {
60
+ static readonly componentId = 'Checkbox'
61
+
62
+ static allowedProps = allowedProps
63
+ static defaultProps = {
64
+ size: 'medium',
65
+ variant: 'simple',
66
+ disabled: false,
67
+ inline: false,
68
+ indeterminate: false,
69
+ readOnly: false,
70
+ labelPlacement: 'end'
71
+ }
72
+
73
+ constructor(props: CheckboxProps) {
74
+ super(props)
75
+
76
+ this.state = {
77
+ focused: false,
78
+ hovered: false,
79
+ checked:
80
+ typeof props.checked === 'undefined'
81
+ ? !!props.defaultChecked
82
+ : undefined
83
+ }
84
+
85
+ if (typeof props.checked === 'undefined') {
86
+ // @ts-expect-error ts-migrate(2339) FIXME: Property 'checked' does not exist on type 'Readonl... Remove this comment to see the full error message
87
+ this.state.checked = !!props.defaultChecked
88
+ }
89
+
90
+ this._defaultId = props.deterministicId!()
91
+ }
92
+ private readonly _defaultId: string
93
+ private _input: HTMLInputElement | null = null
94
+
95
+ ref: Element | null = null
96
+
97
+ handleRef = (el: Element | null) => {
98
+ this.ref = el
99
+ }
100
+
101
+ handleInputRef = (el: HTMLInputElement | null) => {
102
+ this._input = el
103
+ if (typeof this.props.inputRef === 'function') {
104
+ this.props.inputRef(el)
105
+ }
106
+ }
107
+
108
+ componentDidMount() {
109
+ if (this._input) {
110
+ this._input.indeterminate = this.props.indeterminate!
111
+ }
112
+
113
+ this.props.makeStyles?.()
114
+ }
115
+
116
+ componentDidUpdate(prevProps: CheckboxProps) {
117
+ if (prevProps.indeterminate !== this.props.indeterminate && this._input) {
118
+ this._input.indeterminate = this.props.indeterminate || false
119
+ }
120
+
121
+ this.props.makeStyles?.()
122
+ }
123
+
124
+ handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
125
+ const { onChange, disabled, checked, readOnly } = this.props
126
+
127
+ if (disabled || readOnly) {
128
+ e.preventDefault()
129
+ return
130
+ }
131
+
132
+ if (typeof checked === 'undefined') {
133
+ this.setState({ checked: !this.state.checked })
134
+ }
135
+
136
+ if (typeof onChange === 'function') {
137
+ onChange(e)
138
+ }
139
+ }
140
+
141
+ handleKeyDown = (e: React.KeyboardEvent<HTMLInputElement>) => {
142
+ if (this.props.variant === 'toggle' && e.keyCode === keycode.codes.enter) {
143
+ this._input && this._input.click()
144
+ e.preventDefault()
145
+ }
146
+ }
147
+
148
+ handleFocus = () => {
149
+ this.setState({
150
+ focused: true
151
+ })
152
+ }
153
+
154
+ handleBlur = () => {
155
+ this.setState({
156
+ focused: false
157
+ })
158
+ }
159
+
160
+ handleMouseOver = () => {
161
+ !this.props.disabled &&
162
+ this.setState({
163
+ hovered: true
164
+ })
165
+ }
166
+
167
+ handleMouseOut = () => {
168
+ this.setState({
169
+ hovered: false
170
+ })
171
+ }
172
+
173
+ get id() {
174
+ return this.props.id || this._defaultId
175
+ }
176
+
177
+ get checked() {
178
+ return typeof this.props.checked === 'undefined'
179
+ ? this.state.checked
180
+ : this.props.checked
181
+ }
182
+
183
+ get focused() {
184
+ return isActiveElement(this._input)
185
+ }
186
+
187
+ get isError() {
188
+ return !!this.props.messages?.find(
189
+ (m) => m.type === 'error' || m.type === 'newError'
190
+ )
191
+ }
192
+
193
+ get invalid() {
194
+ return !!this.props.messages?.find(
195
+ (m) => m.type === 'newError' || m.type === 'error'
196
+ )
197
+ }
198
+
199
+ focus() {
200
+ this._input && this._input.focus()
201
+ }
202
+
203
+ renderFacade() {
204
+ const {
205
+ size,
206
+ disabled,
207
+ variant,
208
+ label,
209
+ readOnly,
210
+ indeterminate,
211
+ labelPlacement,
212
+ themeOverride,
213
+ isRequired,
214
+ styles
215
+ } = this.props
216
+
217
+ const { hovered, focused } = this.state
218
+
219
+ error(
220
+ !(variant === 'simple' && labelPlacement !== 'end'),
221
+ `[Checkbox] The \`simple\` variant does not support the \`labelPlacement\` property. Use the \`toggle\` variant instead.`
222
+ )
223
+
224
+ if (variant === 'toggle') {
225
+ return (
226
+ <ToggleFacade
227
+ disabled={disabled}
228
+ size={size}
229
+ hovered={hovered}
230
+ focused={focused}
231
+ checked={this.checked}
232
+ readOnly={readOnly}
233
+ labelPlacement={labelPlacement}
234
+ themeOverride={themeOverride as Partial<ToggleFacadeTheme>}
235
+ invalid={this.invalid}
236
+ >
237
+ {label}
238
+ {isRequired && label && (
239
+ <span
240
+ css={this.invalid ? styles?.requiredInvalid : {}}
241
+ aria-hidden={true}
242
+ >
243
+ {' '}
244
+ *
245
+ </span>
246
+ )}
247
+ </ToggleFacade>
248
+ )
249
+ } else {
250
+ return (
251
+ <CheckboxFacade
252
+ disabled={disabled}
253
+ size={size}
254
+ hovered={hovered}
255
+ focused={focused}
256
+ checked={this.checked}
257
+ readOnly={readOnly}
258
+ indeterminate={indeterminate}
259
+ themeOverride={themeOverride as Partial<CheckboxFacadeTheme>}
260
+ invalid={this.invalid}
261
+ >
262
+ {label}
263
+ {isRequired && label && (
264
+ <span
265
+ css={this.invalid ? styles?.requiredInvalid : {}}
266
+ aria-hidden={true}
267
+ >
268
+ {' '}
269
+ *
270
+ </span>
271
+ )}
272
+ </CheckboxFacade>
273
+ )
274
+ }
275
+ }
276
+
277
+ renderMessages() {
278
+ const { messages, styles, variant } = this.props
279
+
280
+ return messages && messages.length > 0 ? (
281
+ <View
282
+ display="block"
283
+ margin="small 0 0"
284
+ css={
285
+ this.isError &&
286
+ (variant === 'toggle'
287
+ ? styles?.indentedToggleError
288
+ : styles?.indentedError)
289
+ }
290
+ >
291
+ <FormFieldMessages messages={messages} />
292
+ </View>
293
+ ) : null
294
+ }
295
+
296
+ render() {
297
+ const {
298
+ disabled,
299
+ readOnly,
300
+ value,
301
+ onKeyDown,
302
+ onFocus,
303
+ onBlur,
304
+ onMouseOver,
305
+ onMouseOut,
306
+ indeterminate,
307
+ variant,
308
+ styles,
309
+ isRequired
310
+ } = this.props
311
+
312
+ const props = omitProps(this.props, Checkbox.allowedProps)
313
+
314
+ error(
315
+ !(variant === 'toggle' && indeterminate),
316
+ `[Checkbox] The \`toggle\` variant does not support the \`indeterminate\` property. Use the \`simple\` variant instead.`
317
+ )
318
+
319
+ return (
320
+ <div
321
+ css={styles?.checkbox}
322
+ /* eslint-disable-next-line jsx-a11y/mouse-events-have-key-events */
323
+ onMouseOver={createChainedFunction(onMouseOver, this.handleMouseOver)}
324
+ /* eslint-disable-next-line jsx-a11y/mouse-events-have-key-events */
325
+ onMouseOut={createChainedFunction(onMouseOut, this.handleMouseOut)}
326
+ ref={this.handleRef}
327
+ data-cid="Checkbox"
328
+ >
329
+ <div css={styles?.container}>
330
+ <input
331
+ {...props}
332
+ id={this.id}
333
+ value={value}
334
+ type="checkbox"
335
+ ref={this.handleInputRef}
336
+ required={isRequired}
337
+ disabled={disabled}
338
+ readOnly={readOnly}
339
+ aria-readonly={readOnly ? true : undefined}
340
+ aria-checked={indeterminate ? 'mixed' : undefined}
341
+ css={styles?.input}
342
+ onClickCapture={(e) => {
343
+ if (readOnly) {
344
+ e.stopPropagation()
345
+ e.preventDefault()
346
+ }
347
+ }}
348
+ onChange={this.handleChange}
349
+ onKeyDown={createChainedFunction(onKeyDown, this.handleKeyDown)}
350
+ onFocus={createChainedFunction(onFocus, this.handleFocus)}
351
+ onBlur={createChainedFunction(onBlur, this.handleBlur)}
352
+ checked={this.checked}
353
+ />
354
+ {/* Prevent the browser from firing a blur on the hidden input during
355
+ mousedown on the label, which would cause Focusable to lose focus
356
+ before the click event is processed. Manually restore focus instead. */}
357
+ {/* eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions */}
358
+ <label
359
+ htmlFor={this.id}
360
+ css={styles?.control}
361
+ onMouseDown={(e) => {
362
+ e.preventDefault()
363
+ this._input?.focus()
364
+ }}
365
+ >
366
+ {this.renderFacade()}
367
+ {this.renderMessages()}
368
+ </label>
369
+ </div>
370
+ </div>
371
+ )
372
+ }
373
+ }
374
+
375
+ export default Checkbox
376
+ export { Checkbox, CheckboxFacade, ToggleFacade }