@ohos-ports/instructure-ui-toggle-details 11.7.5-beta.1

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 (91) hide show
  1. package/CHANGELOG.md +1323 -0
  2. package/LICENSE.md +28 -0
  3. package/README.md +42 -0
  4. package/es/ToggleDetails/v1/index.js +177 -0
  5. package/es/ToggleDetails/v1/props.js +26 -0
  6. package/es/ToggleDetails/v1/styles.js +196 -0
  7. package/es/ToggleDetails/v1/theme.js +75 -0
  8. package/es/ToggleDetails/v2/index.js +181 -0
  9. package/es/ToggleDetails/v2/props.js +26 -0
  10. package/es/ToggleDetails/v2/styles.js +188 -0
  11. package/es/ToggleGroup/v1/index.js +172 -0
  12. package/es/ToggleGroup/v1/props.js +26 -0
  13. package/es/ToggleGroup/v1/styles.js +40 -0
  14. package/es/ToggleGroup/v1/theme.js +41 -0
  15. package/es/ToggleGroup/v2/index.js +178 -0
  16. package/es/ToggleGroup/v2/props.js +26 -0
  17. package/es/ToggleGroup/v2/styles.js +40 -0
  18. package/es/exports/a.js +26 -0
  19. package/es/exports/b.js +26 -0
  20. package/lib/ToggleDetails/v1/index.js +184 -0
  21. package/lib/ToggleDetails/v1/props.js +31 -0
  22. package/lib/ToggleDetails/v1/styles.js +202 -0
  23. package/lib/ToggleDetails/v1/theme.js +81 -0
  24. package/lib/ToggleDetails/v2/index.js +187 -0
  25. package/lib/ToggleDetails/v2/props.js +31 -0
  26. package/lib/ToggleDetails/v2/styles.js +194 -0
  27. package/lib/ToggleGroup/v1/index.js +180 -0
  28. package/lib/ToggleGroup/v1/props.js +31 -0
  29. package/lib/ToggleGroup/v1/styles.js +46 -0
  30. package/lib/ToggleGroup/v1/theme.js +47 -0
  31. package/lib/ToggleGroup/v2/index.js +185 -0
  32. package/lib/ToggleGroup/v2/props.js +31 -0
  33. package/lib/ToggleGroup/v2/styles.js +46 -0
  34. package/lib/exports/a.js +19 -0
  35. package/lib/exports/b.js +19 -0
  36. package/lib/package.json +1 -0
  37. package/package.json +94 -0
  38. package/src/ToggleDetails/v1/README.md +233 -0
  39. package/src/ToggleDetails/v1/index.tsx +204 -0
  40. package/src/ToggleDetails/v1/props.ts +114 -0
  41. package/src/ToggleDetails/v1/styles.ts +190 -0
  42. package/src/ToggleDetails/v1/theme.ts +80 -0
  43. package/src/ToggleDetails/v2/README.md +233 -0
  44. package/src/ToggleDetails/v2/index.tsx +206 -0
  45. package/src/ToggleDetails/v2/props.ts +115 -0
  46. package/src/ToggleDetails/v2/styles.ts +188 -0
  47. package/src/ToggleGroup/v1/README.md +93 -0
  48. package/src/ToggleGroup/v1/index.tsx +206 -0
  49. package/src/ToggleGroup/v1/props.ts +115 -0
  50. package/src/ToggleGroup/v1/styles.ts +47 -0
  51. package/src/ToggleGroup/v1/theme.ts +43 -0
  52. package/src/ToggleGroup/v2/README.md +93 -0
  53. package/src/ToggleGroup/v2/index.tsx +214 -0
  54. package/src/ToggleGroup/v2/props.ts +115 -0
  55. package/src/ToggleGroup/v2/styles.ts +47 -0
  56. package/src/exports/a.ts +29 -0
  57. package/src/exports/b.ts +29 -0
  58. package/tsconfig.build.json +26 -0
  59. package/tsconfig.json +4 -0
  60. package/types/ToggleDetails/v1/index.d.ts +56 -0
  61. package/types/ToggleDetails/v1/index.d.ts.map +1 -0
  62. package/types/ToggleDetails/v1/props.d.ts +55 -0
  63. package/types/ToggleDetails/v1/props.d.ts.map +1 -0
  64. package/types/ToggleDetails/v1/styles.d.ts +15 -0
  65. package/types/ToggleDetails/v1/styles.d.ts.map +1 -0
  66. package/types/ToggleDetails/v1/theme.d.ts +10 -0
  67. package/types/ToggleDetails/v1/theme.d.ts.map +1 -0
  68. package/types/ToggleDetails/v2/index.d.ts +55 -0
  69. package/types/ToggleDetails/v2/index.d.ts.map +1 -0
  70. package/types/ToggleDetails/v2/props.d.ts +56 -0
  71. package/types/ToggleDetails/v2/props.d.ts.map +1 -0
  72. package/types/ToggleDetails/v2/styles.d.ts +16 -0
  73. package/types/ToggleDetails/v2/styles.d.ts.map +1 -0
  74. package/types/ToggleGroup/v1/index.d.ts +55 -0
  75. package/types/ToggleGroup/v1/index.d.ts.map +1 -0
  76. package/types/ToggleGroup/v1/props.d.ts +65 -0
  77. package/types/ToggleGroup/v1/props.d.ts.map +1 -0
  78. package/types/ToggleGroup/v1/styles.d.ts +15 -0
  79. package/types/ToggleGroup/v1/styles.d.ts.map +1 -0
  80. package/types/ToggleGroup/v1/theme.d.ts +10 -0
  81. package/types/ToggleGroup/v1/theme.d.ts.map +1 -0
  82. package/types/ToggleGroup/v2/index.d.ts +54 -0
  83. package/types/ToggleGroup/v2/index.d.ts.map +1 -0
  84. package/types/ToggleGroup/v2/props.d.ts +66 -0
  85. package/types/ToggleGroup/v2/props.d.ts.map +1 -0
  86. package/types/ToggleGroup/v2/styles.d.ts +15 -0
  87. package/types/ToggleGroup/v2/styles.d.ts.map +1 -0
  88. package/types/exports/a.d.ts +5 -0
  89. package/types/exports/a.d.ts.map +1 -0
  90. package/types/exports/b.d.ts +5 -0
  91. package/types/exports/b.d.ts.map +1 -0
@@ -0,0 +1,190 @@
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 { keyframes } from '@instructure/emotion'
26
+
27
+ import type { ToggleDetailsTheme } from '@instructure/shared-types'
28
+ import type {
29
+ ToggleDetailsProps,
30
+ ToggleDetailsStyleProps,
31
+ ToggleDetailsStyle
32
+ } from './props'
33
+
34
+ const contentAnimation = keyframes`
35
+ to {
36
+ opacity: 1;
37
+ }`
38
+
39
+ /**
40
+ * ---
41
+ * private: true
42
+ * ---
43
+ * Generates the style object from the theme and provided additional information
44
+ * @param {Object} componentTheme The theme variable object.
45
+ * @param {Object} props the props of the component, the style is applied to
46
+ * @param {Object} state the state of the component, the style is applied to
47
+ * @return {Object} The final style object, which will be used in the component
48
+ */
49
+ const generateStyle = (
50
+ componentTheme: ToggleDetailsTheme,
51
+ props: ToggleDetailsProps,
52
+ _state: ToggleDetailsStyleProps
53
+ ): ToggleDetailsStyle => {
54
+ const { fluidWidth, iconPosition, size, variant } = props
55
+
56
+ const positionIconAtEnd =
57
+ iconPosition === 'end' && (variant === 'filled' || fluidWidth)
58
+ const fluidWidthStyles = fluidWidth ? { display: 'block', width: '100%' } : {}
59
+
60
+ const iconPositionStyles = {
61
+ start: {
62
+ marginInlineEnd: componentTheme.iconMargin,
63
+ marginInlineStart: 0
64
+ },
65
+ end: {
66
+ marginInlineStart: componentTheme.iconMargin,
67
+ marginInlineEnd: 0
68
+ }
69
+ }
70
+
71
+ const fontSizeStyles = {
72
+ small: { fontSize: componentTheme.fontSizeSmall },
73
+ medium: { fontSize: componentTheme.fontSizeMedium },
74
+ large: { fontSize: componentTheme.fontSizeLarge }
75
+ }
76
+
77
+ const iconSizeStyles = {
78
+ small: { fontSize: componentTheme.smallIconSize },
79
+ medium: { fontSize: componentTheme.mediumIconSize },
80
+ large: { fontSize: componentTheme.largeIconSize }
81
+ }
82
+
83
+ const indentDetailsStyles =
84
+ iconPosition === 'start' && !fluidWidth
85
+ ? {
86
+ small: {
87
+ paddingInlineStart: `calc(${componentTheme.smallIconSize} + ${componentTheme.togglePadding})`,
88
+ paddingInlineEnd: '0'
89
+ },
90
+ medium: {
91
+ paddingInlineStart: `calc(${componentTheme.mediumIconSize} + ${componentTheme.togglePadding})`,
92
+ paddingInlineEnd: '0'
93
+ },
94
+ large: {
95
+ paddingInlineStart: `calc(${componentTheme.largeIconSize} + ${componentTheme.togglePadding})`,
96
+ paddingInlineEnd: '0'
97
+ }
98
+ }
99
+ : {
100
+ small: {},
101
+ medium: {},
102
+ large: {}
103
+ }
104
+
105
+ return {
106
+ toggleDetails: {
107
+ label: 'toggleDetails',
108
+ fontFamily: componentTheme.fontFamily,
109
+ fontWeight: componentTheme.fontWeight,
110
+ lineHeight: componentTheme.lineHeight
111
+ },
112
+ summary: {
113
+ label: 'toggleDetails__summary',
114
+ position: 'relative',
115
+ display: 'flex',
116
+ alignItems: 'center',
117
+ boxSizing: 'border-box',
118
+ textAlign: 'start'
119
+ },
120
+ summaryText: {
121
+ label: 'toggleDetails__summaryText',
122
+ minWidth: '0.0625rem',
123
+ flexGrow: 1,
124
+ ...(positionIconAtEnd ? { flex: 1 } : {})
125
+ },
126
+ toggle: {
127
+ label: 'toggleDetails__toggle',
128
+ fontFamily: componentTheme.fontFamily,
129
+ appearance: 'none',
130
+ cursor: 'pointer',
131
+ userSelect: 'none',
132
+ touchAction: 'manipulation',
133
+ position: 'relative',
134
+ overflow: 'visible',
135
+ boxSizing: 'border-box',
136
+ padding: '0',
137
+ margin: '0',
138
+ outline: 'none',
139
+ border: 'none',
140
+ background: 'transparent',
141
+ color: componentTheme.textColor,
142
+ '&::-moz-focus-inner': { padding: '0', margin: '0' },
143
+ '&::before': {
144
+ content: '""',
145
+ position: 'absolute',
146
+ top: '-0.375rem',
147
+ left: '-0.375rem',
148
+ right: '-0.375rem',
149
+ bottom: '-0.375rem',
150
+ border: `${componentTheme.toggleBorderWidth} ${componentTheme.toggleBorderStyle} ${componentTheme.toggleFocusBorderColor}`,
151
+ borderRadius: `calc(${componentTheme.toggleBorderRadius} * 1.5)`,
152
+ opacity: 0,
153
+ pointerEvents: 'none'
154
+ },
155
+ '&:focus': { '&::before': { opacity: 1 } },
156
+ '&:focus, &:hover, &:active': {
157
+ textDecoration: 'none',
158
+ color: componentTheme.textColor
159
+ },
160
+ textDecoration: 'none',
161
+ ...fluidWidthStyles,
162
+ ...fontSizeStyles[size!]
163
+ },
164
+ icon: {
165
+ label: 'toggleDetails__icon',
166
+ '& > svg': {
167
+ display: 'block' /* fix vertical alignment of icon */
168
+ },
169
+ ...iconPositionStyles[iconPosition!],
170
+ ...iconSizeStyles[size!]
171
+ },
172
+ details: {
173
+ label: 'toggleDetails__details',
174
+ boxSizing: 'border-box',
175
+ paddingTop: componentTheme.togglePadding,
176
+ color: componentTheme.textColor,
177
+ ...fontSizeStyles[size!],
178
+ ...indentDetailsStyles[size!]
179
+ },
180
+ content: {
181
+ label: 'toggleDetails__content',
182
+ opacity: 0.01,
183
+ animationName: contentAnimation,
184
+ animationFillMode: 'forwards',
185
+ animationDuration: '.3s'
186
+ }
187
+ }
188
+ }
189
+
190
+ export default generateStyle
@@ -0,0 +1,80 @@
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 { Theme, ThemeSpecificStyle } from '@instructure/ui-themes'
26
+ import { ToggleDetailsTheme } from '@instructure/shared-types'
27
+
28
+ /**
29
+ * Generates the theme object for the component from the theme and provided additional information
30
+ * @param {Object} theme The actual theme object.
31
+ * @return {Object} The final theme object with the overrides and component variables
32
+ */
33
+ const generateComponentTheme = (theme: Theme): ToggleDetailsTheme => {
34
+ const { colors, spacing, borders, typography, key: themeName } = theme
35
+
36
+ const themeSpecificStyle: ThemeSpecificStyle<ToggleDetailsTheme> = {
37
+ canvas: {
38
+ toggleFocusBorderColor: theme['ic-brand-primary'],
39
+ iconColor: theme['ic-brand-font-color-dark'],
40
+ textColor: theme['ic-brand-font-color-dark']
41
+ }
42
+ }
43
+
44
+ const componentVariables: ToggleDetailsTheme = {
45
+ fontFamily: typography?.fontFamily,
46
+ fontWeight: typography?.fontWeightNormal,
47
+ lineHeight: typography?.lineHeight,
48
+ textColor: colors?.contrasts?.grey125125,
49
+
50
+ fontSizeSmall: typography?.fontSizeSmall,
51
+ fontSizeMedium: typography?.fontSizeMedium,
52
+ fontSizeLarge: typography?.fontSizeLarge,
53
+
54
+ smallIconSize: '0.5rem',
55
+ mediumIconSize: '0.75rem',
56
+ largeIconSize: '1rem',
57
+ iconMargin: spacing?.xxSmall,
58
+ iconColor: colors?.contrasts?.grey125125,
59
+
60
+ togglePadding: spacing?.xxSmall,
61
+ toggleBorderRadius: borders?.radiusMedium,
62
+ toggleBorderWidth: borders?.widthMedium,
63
+ toggleBorderStyle: borders?.style,
64
+ toggleFocusBorderColor: colors?.contrasts?.blue4570,
65
+
66
+ filledBackgroundColor: colors?.contrasts?.grey1111,
67
+ filledBorderWidth: borders?.widthSmall,
68
+ filledBorderStyle: borders?.style,
69
+ filledBorderColor: colors?.contrasts?.grey1424,
70
+ filledBorderRadius: borders?.radiusMedium,
71
+ filledPadding: spacing?.small
72
+ }
73
+
74
+ return {
75
+ ...componentVariables,
76
+ ...themeSpecificStyle[themeName]
77
+ }
78
+ }
79
+
80
+ export default generateComponentTheme
@@ -0,0 +1,233 @@
1
+ ---
2
+ describes: ToggleDetails
3
+ ---
4
+
5
+ The ToggleDetails component can be used to show/hide content in response to user action.
6
+
7
+ By default, ToggleDetails content is hidden. To override, pass in the `defaultExpanded` prop.
8
+
9
+ ```js
10
+ ---
11
+ type: example
12
+ ---
13
+ <ToggleDetails
14
+ summary="Click to hide me!"
15
+ defaultExpanded
16
+ >
17
+ <Text weight="bold">I am expanded!</Text> {lorem.paragraph()}
18
+ </ToggleDetails>
19
+ ```
20
+
21
+ ToggleDetails can be controlled:
22
+
23
+ ```js
24
+ ---
25
+ type: example
26
+ ---
27
+ const Example = () => {
28
+ const [expanded, setExpanded] = useState(true)
29
+
30
+ const handleChange = (event, expanded) => {
31
+ setExpanded(expanded)
32
+ }
33
+
34
+ const handleToggle = () => {
35
+ setExpanded((prevExpanded) => !prevExpanded)
36
+ }
37
+
38
+ return (
39
+ <div>
40
+ <Button onClick={handleToggle}>
41
+ <div aria-live="polite">
42
+ This Button {expanded ? 'Collapses' : 'Expands'}
43
+ </div>
44
+ </Button>
45
+ <br />
46
+ <br />
47
+ <ToggleDetails
48
+ summary="Click to hide me!"
49
+ expanded={expanded}
50
+ onToggle={handleChange}
51
+ >
52
+ <Text weight="bold">I am controlled and expanded!</Text>
53
+ {lorem.paragraph()}
54
+ </ToggleDetails>
55
+ </div>
56
+ )
57
+ }
58
+
59
+ render(<Example />)
60
+ ```
61
+
62
+ Setting ToggleDetails to `filled` will make the toggle use a full-width [Button](Button) component.
63
+
64
+ ```js
65
+ ---
66
+ type: example
67
+ ---
68
+ <ToggleDetails
69
+ variant="filled"
70
+ summary="Click to expand me!"
71
+ >
72
+ {lorem.paragraph()}
73
+ </ToggleDetails>
74
+ ```
75
+
76
+ ### Icon size / summary text formatting
77
+
78
+ Icon size can be adjusted using the `size` prop with small, medium, and large options.
79
+
80
+ The `summary` prop accepts any node, allowing you to format the summary text as
81
+ you see fit. In these examples, we are formatting it with the
82
+ [Text](Text) component.
83
+
84
+ ```js
85
+ ---
86
+ type: example
87
+ ---
88
+ <div>
89
+ <ToggleDetails
90
+ size="small"
91
+ summary="Small icon"
92
+ >
93
+ {lorem.paragraph()}
94
+ </ToggleDetails>
95
+
96
+ <br />
97
+
98
+ <ToggleDetails summary="Medium icon">
99
+ {lorem.paragraph()}
100
+ </ToggleDetails>
101
+
102
+ <br />
103
+
104
+ <ToggleDetails
105
+ size="large"
106
+ summary="Large icon"
107
+ >
108
+ {lorem.paragraph()}
109
+ </ToggleDetails>
110
+ </div>
111
+ ```
112
+
113
+ ### Icon positioning and block display
114
+
115
+ The `iconPosition` prop determines if the icon comes before or after the summary.
116
+
117
+ When the `fluidWidth` prop is set, the toggle fills the width of its
118
+ container.
119
+
120
+ ```js
121
+ ---
122
+ type: example
123
+ ---
124
+ <ToggleDetails
125
+ summary="Block display"
126
+ iconPosition="end"
127
+ defaultExpanded
128
+ fluidWidth
129
+ >
130
+ <Text>
131
+ {lorem.paragraph()}
132
+ </Text>
133
+ </ToggleDetails>
134
+ ```
135
+
136
+ ### Example to demonstrate more properties
137
+
138
+ ```js
139
+ ---
140
+ type: example
141
+ ---
142
+ const Example = () => {
143
+ const [state, setState] = useState({
144
+ expanded: true,
145
+ fluidWidth: true,
146
+ iconPosition: 'start',
147
+ size: 'small',
148
+ variant: 'default'
149
+ });
150
+
151
+ const handleChange = (field, value) => setState(prevState => ({ ...prevState, [field]: value }));
152
+ const handleToggle = () => setState(prevState => ({ ...prevState, expanded: !prevState.expanded }));
153
+
154
+ const renderOptions = () => {
155
+ const { fluidWidth, iconPosition, size, variant } = state;
156
+ const options = [
157
+ { name: 'iconPosition', values: ['start', 'end'] },
158
+ { name: 'size', values: ['small', 'medium', 'large'] },
159
+ { name: 'variant', values: ['default', 'filled'] },
160
+ ];
161
+
162
+ return (
163
+ <Flex alignItems="start">
164
+ {options.map(({ name, values }) => (
165
+ <Flex.Item margin="general.spaceMd" key={name}>
166
+ <RadioInputGroup
167
+ name={name}
168
+ description={name}
169
+ value={state[name]}
170
+ onChange={(e, value) => handleChange(name, value)}
171
+ >
172
+ {values.map(val => (
173
+ <RadioInput label={val} value={val} key={val} />
174
+ ))}
175
+ </RadioInputGroup>
176
+ </Flex.Item>
177
+ ))}
178
+ <Flex.Item margin="general.spaceMd">
179
+ <Checkbox
180
+ label="fluidWidth"
181
+ checked={fluidWidth}
182
+ onChange={() => handleChange('fluidWidth', !fluidWidth)}
183
+ />
184
+ </Flex.Item>
185
+ </Flex>
186
+ );
187
+ };
188
+
189
+ const { expanded, iconPosition, size, variant, fluidWidth } = state;
190
+
191
+ return (
192
+ <div>
193
+ {renderOptions()}
194
+ <Button onClick={handleToggle}>
195
+ This Button {expanded ? 'Collapses' : 'Expands'}
196
+ </Button>
197
+ <br />
198
+ <br />
199
+ <ToggleDetails
200
+ summary="Click to hide me!"
201
+ expanded={expanded}
202
+ onToggle={(_, expanded) => handleChange('expanded', expanded)}
203
+ fluidWidth={fluidWidth}
204
+ iconPosition={iconPosition}
205
+ size={size}
206
+ variant={variant}
207
+ >
208
+ <span>
209
+ I am controlled and expanded!I am controlled and expanded!I am controlled and expanded!I am controlled and expanded!I am controlled and expanded!I am controlled and expanded!I am controlled and expanded!I am controlled and expanded!I am controlled and expanded!I am controlled and expanded!I am controlled and expanded!I am controlled and expanded!I am controlled and expanded!I am controlled and expanded!I am controlled and expanded!I am controlled and expanded!I am controlled and expanded!I am controlled and expanded!I am controlled and expanded!I am controlled and expanded!I am controlled and expanded!I am controlled and expanded!I am controlled and expanded!I am controlled and expanded!I am controlled and expanded!
210
+ </span>
211
+ </ToggleDetails>
212
+ </div>
213
+ );
214
+ };
215
+
216
+ render(<Example />)
217
+ ```
218
+
219
+ ### Guidelines
220
+
221
+ ```js
222
+ ---
223
+ type: embed
224
+ ---
225
+ <Guidelines>
226
+ <Figure recommendation="yes" title="Do">
227
+ <Figure.Item>Use ToggleDetails when descriptive text is longer than a short phrase</Figure.Item>
228
+ </Figure>
229
+ <Figure recommendation="no" title="Don't">
230
+ <Figure.Item>Nest ToggleDetails within another ToggleDetails</Figure.Item>
231
+ </Figure>
232
+ </Guidelines>
233
+ ```
@@ -0,0 +1,206 @@
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, createRef } from 'react'
26
+ import { Button } from '@instructure/ui-buttons/latest'
27
+ import {
28
+ ChevronRightInstUIIcon,
29
+ ChevronDownInstUIIcon
30
+ } from '@instructure/ui-icons'
31
+ import { Expandable } from '@instructure/ui-expandable/latest'
32
+ import { omitProps, pickProps } from '@instructure/ui-react-utils'
33
+ import { isActiveElement } from '@instructure/ui-dom-utils'
34
+
35
+ import { withStyleNew } from '@instructure/emotion'
36
+ import generateStyle from './styles.js'
37
+ import type { ToggleDetailsProps } from './props'
38
+ import { allowedProps } from './props.js'
39
+ import type { ExpandableToggleProps } from '@instructure/ui-expandable/latest'
40
+ import type { ViewProps } from '@instructure/ui-view/latest'
41
+
42
+ /**
43
+ ---
44
+ category: components
45
+ ---
46
+ **/
47
+ @withStyleNew(generateStyle)
48
+ class ToggleDetails extends Component<ToggleDetailsProps> {
49
+ static displayName = 'ToggleDetails'
50
+ static readonly componentId = 'ToggleDetails'
51
+ static allowedProps = allowedProps
52
+
53
+ static defaultProps = {
54
+ variant: 'default',
55
+ size: 'medium',
56
+ fluidWidth: false,
57
+ icon: ChevronRightInstUIIcon,
58
+ iconExpanded: ChevronDownInstUIIcon,
59
+ iconPosition: 'start',
60
+ defaultExpanded: false,
61
+ children: null
62
+ }
63
+
64
+ _buttonNode: HTMLElement | null = null
65
+ ref = createRef<HTMLDivElement>()
66
+
67
+ // TODO this solution was generated with Gemini, should be tested if it works as fine as before
68
+ setButtonRef = (node: Element | null) => {
69
+ this._buttonNode = node as HTMLElement | null
70
+ }
71
+
72
+ get focused() {
73
+ return isActiveElement(this._buttonNode)
74
+ }
75
+
76
+ focus() {
77
+ this._buttonNode?.focus()
78
+ }
79
+
80
+ componentDidMount() {
81
+ this.props.makeStyles?.()
82
+ }
83
+
84
+ componentDidUpdate() {
85
+ this.props.makeStyles?.()
86
+ }
87
+
88
+ renderSummary(expanded: boolean) {
89
+ const { summary, iconPosition } = this.props
90
+
91
+ return (
92
+ <span css={this.props.styles?.summary}>
93
+ {iconPosition === 'start' && this.renderIcon(expanded)}
94
+ <span css={this.props.styles?.summaryText}>{summary}</span>
95
+ {iconPosition === 'end' && this.renderIcon(expanded)}
96
+ </span>
97
+ )
98
+ }
99
+
100
+ renderToggle(
101
+ toggleProps: ReturnType<ExpandableToggleProps>,
102
+ expanded: boolean
103
+ ) {
104
+ const { variant } = this.props
105
+ const tProps = this.props.children
106
+ ? toggleProps
107
+ : { onClick: toggleProps.onClick }
108
+ const props = {
109
+ ...omitProps(this.props, ToggleDetails.allowedProps),
110
+ ...tProps,
111
+ children: this.renderSummary(expanded)
112
+ } as Record<string, unknown>
113
+ const summary = this.renderSummary(expanded)
114
+
115
+ if (variant === 'filled') {
116
+ return (
117
+ <Button
118
+ {...props}
119
+ display="block"
120
+ textAlign="start"
121
+ elementRef={this.setButtonRef}
122
+ >
123
+ {summary}
124
+ </Button>
125
+ )
126
+ } else if (props.href) {
127
+ return (
128
+ <a {...props} css={this.props.styles?.toggle} ref={this.setButtonRef}>
129
+ {summary}
130
+ </a>
131
+ )
132
+ } else {
133
+ return (
134
+ <button
135
+ {...props}
136
+ type="button"
137
+ css={this.props.styles?.toggle}
138
+ ref={this.setButtonRef}
139
+ >
140
+ {summary}
141
+ </button>
142
+ )
143
+ }
144
+ }
145
+
146
+ renderIcon(expanded: boolean) {
147
+ const Icon = expanded ? this.props.iconExpanded : this.props.icon
148
+ const iconSize = this.props.size === 'large' ? 'lg' : 'md'
149
+ const iconColor =
150
+ this.props.variant === 'filled' ? 'actionSecondaryBaseColor' : 'baseColor'
151
+
152
+ return this.props.children && Icon ? (
153
+ <span css={this.props.styles?.icon}>
154
+ <Icon size={iconSize} color={iconColor} />
155
+ </span>
156
+ ) : null
157
+ }
158
+
159
+ renderDetails(expanded: boolean, detailsProps: { id: string }) {
160
+ if (!this.props.children) return null
161
+ const expandedStyles = expanded ? { display: 'block' } : { display: 'none' }
162
+ return (
163
+ <div {...detailsProps} css={[this.props.styles?.details, expandedStyles]}>
164
+ {expanded && this.renderContent()}
165
+ </div>
166
+ )
167
+ }
168
+
169
+ renderContent() {
170
+ return <div css={this.props.styles?.content}>{this.props.children}</div>
171
+ }
172
+
173
+ handleToggle = (
174
+ event: React.KeyboardEvent<ViewProps> | React.MouseEvent<ViewProps>,
175
+ expanded: boolean
176
+ ) => {
177
+ if (typeof this.props.onToggle === 'function') {
178
+ this.props.onToggle(event, expanded)
179
+ }
180
+ }
181
+
182
+ render() {
183
+ return (
184
+ <Expandable
185
+ {...pickProps(this.props, Expandable.allowedProps)}
186
+ onToggle={this.handleToggle}
187
+ >
188
+ {({ expanded, getToggleProps, getDetailsProps }) => {
189
+ return (
190
+ <div
191
+ css={this.props.styles?.toggleDetails}
192
+ ref={this.ref}
193
+ data-cid="ToggleDetails"
194
+ >
195
+ {this.renderToggle(getToggleProps(), expanded)}
196
+ {this.renderDetails(expanded, getDetailsProps())}
197
+ </div>
198
+ )
199
+ }}
200
+ </Expandable>
201
+ )
202
+ }
203
+ }
204
+
205
+ export default ToggleDetails
206
+ export { ToggleDetails }