@okta/odyssey-react-mui 1.9.8 → 1.9.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@okta/odyssey-react-mui",
3
- "version": "1.9.8",
3
+ "version": "1.9.10",
4
4
  "description": "React MUI components for Odyssey, Okta's design system",
5
5
  "author": "Okta, Inc.",
6
6
  "license": "Apache-2.0",
@@ -51,7 +51,7 @@
51
51
  "@mui/system": "^5.14.9",
52
52
  "@mui/utils": "^5.11.2",
53
53
  "@mui/x-date-pickers": "^5.0.15",
54
- "@okta/odyssey-design-tokens": "1.9.8",
54
+ "@okta/odyssey-design-tokens": "1.9.10",
55
55
  "date-fns": "^2.30.0",
56
56
  "i18next": "^23.5.1",
57
57
  "material-react-table": "^2.0.2",
@@ -63,5 +63,5 @@
63
63
  "react": ">=17 <19",
64
64
  "react-dom": ">=17 <19"
65
65
  },
66
- "gitHead": "b7e137230a43e2d028a0c5026b2ed6c9d70f790b"
66
+ "gitHead": "4a4a98d1b9e421122332cb0635d823189ee4e72b"
67
67
  }
@@ -19,6 +19,7 @@ import { I18nextProvider } from "react-i18next";
19
19
  import { getTypedObjectKeys } from "./getTypedObjectKeys";
20
20
 
21
21
  export type OdysseyI18nResourceKeys = (typeof resources)["en"];
22
+ export const odysseyI18nResourceKeysList = getTypedObjectKeys(resources["en"]);
22
23
 
23
24
  export type TranslationOverrides<
24
25
  SupportedLanguages extends string = DefaultSupportedLanguages
package/src/index.ts CHANGED
@@ -96,6 +96,7 @@ export * from "./TextField";
96
96
  export * from "./theme";
97
97
  export * from "./Toast";
98
98
  export * from "./ToastStack";
99
+ export * from "./labs/Switch";
99
100
  export * from "./Tooltip";
100
101
  export * from "./Typography";
101
102
  export * from "./useUniqueId";
@@ -0,0 +1,246 @@
1
+ /*!
2
+ * Copyright (c) 2022-present, Okta, Inc. and/or its affiliates. All rights reserved.
3
+ * The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.")
4
+ *
5
+ * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
6
+ * Unless required by applicable law or agreed to in writing, software
7
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
8
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9
+ *
10
+ * See the License for the specific language governing permissions and limitations under the License.
11
+ */
12
+
13
+ import {
14
+ memo,
15
+ SyntheticEvent,
16
+ useCallback,
17
+ useEffect,
18
+ useMemo,
19
+ useRef,
20
+ useState,
21
+ } from "react";
22
+ import {
23
+ Switch as MuiSwitch,
24
+ SwitchProps as MuiSwitchProps,
25
+ FormControlLabel,
26
+ } from "@mui/material";
27
+
28
+ const { CONTROLLED } = ComponentControlledState;
29
+ import { useOdysseyDesignTokens } from "../OdysseyDesignTokensContext";
30
+ import { Box } from "../Box";
31
+ import { FieldComponentProps } from "../FieldComponentProps";
32
+ import { FieldHint } from "../FieldHint";
33
+ import type { SeleniumProps } from "../SeleniumProps";
34
+ import { useUniqueId } from "../useUniqueId";
35
+ import { ComponentControlledState, getControlState } from "../inputUtils";
36
+ import { CheckedFieldProps } from "../FormCheckedProps";
37
+
38
+ type OnChangeCallbackArguments = {
39
+ checked: boolean;
40
+ value: string;
41
+ };
42
+
43
+ export type SwitchProps = {
44
+ /**
45
+ * if `true`, the label and switch span entire containing element's width
46
+ */
47
+ isFullWidth?: boolean;
48
+ /**
49
+ * The label text for the Switch
50
+ */
51
+ label: string;
52
+ /**
53
+ * The change event handler for the Switch
54
+ */
55
+ onChange?: ({ checked, value }: OnChangeCallbackArguments) => void;
56
+ /**
57
+ * The value attribute of the Switch
58
+ */
59
+ value: string;
60
+ } & Pick<
61
+ FieldComponentProps,
62
+ "hint" | "id" | "isFullWidth" | "isDisabled" | "name"
63
+ > &
64
+ CheckedFieldProps<MuiSwitchProps> &
65
+ SeleniumProps;
66
+
67
+ type SwitchLabelProps = {
68
+ checked: boolean;
69
+ hint: SwitchProps["hint"];
70
+ hintId?: string;
71
+ isFullWidth: SwitchProps["isFullWidth"];
72
+ label: SwitchProps["label"];
73
+ };
74
+
75
+ const SwitchLabel = ({
76
+ checked,
77
+ hint,
78
+ hintId,
79
+ isFullWidth,
80
+ label,
81
+ }: SwitchLabelProps) => {
82
+ const odysseyDesignTokens = useOdysseyDesignTokens();
83
+
84
+ return (
85
+ <>
86
+ <Box
87
+ sx={{
88
+ display: "flex",
89
+ alignItems: "center",
90
+ flexWrap: "wrap",
91
+ gap: odysseyDesignTokens.Spacing1,
92
+ margin: 0,
93
+ maxWidth: isFullWidth
94
+ ? "100%"
95
+ : odysseyDesignTokens.TypographyLineLengthMax,
96
+ fontWeight: odysseyDesignTokens.TypographyWeightBodyBold,
97
+ }}
98
+ >
99
+ {label}
100
+ <Box
101
+ sx={{
102
+ padding: "2px 4px",
103
+ backgroundColor: checked
104
+ ? odysseyDesignTokens.PaletteSuccessLighter
105
+ : odysseyDesignTokens.HueNeutral100,
106
+ borderRadius: odysseyDesignTokens.BorderRadiusMain,
107
+ color: checked
108
+ ? odysseyDesignTokens.PaletteSuccessText
109
+ : odysseyDesignTokens.HueNeutral700,
110
+ fontWeight: odysseyDesignTokens.TypographyWeightBodyBold,
111
+ fontSize: odysseyDesignTokens.TypographyScale0,
112
+ lineHeight: odysseyDesignTokens.TypographyLineHeightOverline,
113
+ transitionProperty: "background-color, color",
114
+ transitionDuration: odysseyDesignTokens.TransitionDurationMain,
115
+ }}
116
+ >
117
+ {checked ? "Active" : "Inactive"}
118
+ </Box>
119
+ </Box>
120
+ {hint && <FieldHint id={hintId} text={hint} />}
121
+ </>
122
+ );
123
+ };
124
+
125
+ const Switch = ({
126
+ hint,
127
+ id: _id,
128
+ isChecked,
129
+ isDefaultChecked,
130
+ isDisabled,
131
+ isFullWidth = false,
132
+ label,
133
+ name: _name,
134
+ onChange,
135
+ testId,
136
+ value = "Something",
137
+ }: SwitchProps) => {
138
+ const odysseyDesignTokens = useOdysseyDesignTokens();
139
+ const controlledStateRef = useRef(
140
+ getControlState({
141
+ controlledValue: isChecked,
142
+ uncontrolledValue: isDefaultChecked,
143
+ })
144
+ );
145
+ const inputValues = useMemo(() => {
146
+ if (controlledStateRef.current === CONTROLLED) {
147
+ return { checked: isChecked };
148
+ }
149
+ return { defaultChecked: isDefaultChecked };
150
+ }, [isDefaultChecked, isChecked]);
151
+
152
+ const [internalSwitchChecked, setInternalSwitchChecked] = useState(
153
+ controlledStateRef.current === CONTROLLED
154
+ ? Boolean(isChecked)
155
+ : Boolean(isDefaultChecked)
156
+ );
157
+
158
+ useEffect(() => {
159
+ if (controlledStateRef.current === CONTROLLED) {
160
+ setInternalSwitchChecked(Boolean(isChecked));
161
+ }
162
+ }, [isChecked]);
163
+
164
+ const id = useUniqueId(_id);
165
+
166
+ const hintId = hint ? `${id}-hint` : undefined;
167
+ const labelElementId = `${id}-label`;
168
+
169
+ const handleOnChange = useCallback(
170
+ (_: SyntheticEvent<Element, Event>, checked: boolean) => {
171
+ setInternalSwitchChecked(checked);
172
+ onChange?.({ checked, value });
173
+ },
174
+ [onChange, setInternalSwitchChecked, value]
175
+ );
176
+
177
+ const renderSwitchComponent = useMemo(
178
+ () => (
179
+ <MuiSwitch
180
+ {...inputValues}
181
+ disabled={isDisabled}
182
+ disableRipple
183
+ inputProps={{
184
+ "aria-checked": internalSwitchChecked,
185
+ "aria-describedby": hintId,
186
+ "aria-label": label,
187
+ "aria-labelledby": labelElementId,
188
+ }}
189
+ name={_name ?? id}
190
+ onChange={handleOnChange}
191
+ />
192
+ ),
193
+ [
194
+ handleOnChange,
195
+ hintId,
196
+ inputValues,
197
+ internalSwitchChecked,
198
+ id,
199
+ isDisabled,
200
+ label,
201
+ labelElementId,
202
+ _name,
203
+ ]
204
+ );
205
+
206
+ return (
207
+ <Box
208
+ sx={{
209
+ marginBlockEnd: odysseyDesignTokens.Spacing2,
210
+ }}
211
+ >
212
+ <FormControlLabel
213
+ checked={internalSwitchChecked}
214
+ control={renderSwitchComponent}
215
+ data-se={testId}
216
+ disabled={isDisabled}
217
+ id={labelElementId}
218
+ label={
219
+ <SwitchLabel
220
+ checked={internalSwitchChecked}
221
+ hint={hint}
222
+ hintId={hintId}
223
+ isFullWidth={isFullWidth}
224
+ label={label}
225
+ />
226
+ }
227
+ labelPlacement="start"
228
+ sx={{
229
+ justifyContent: "space-between",
230
+ alignItems: "flex-start",
231
+ gap: odysseyDesignTokens.Spacing4,
232
+ width: "100%",
233
+ maxWidth: isFullWidth
234
+ ? "100%"
235
+ : odysseyDesignTokens.TypographyLineLengthMax,
236
+ }}
237
+ value={value}
238
+ />
239
+ </Box>
240
+ );
241
+ };
242
+
243
+ const MemoizedSwitch = memo(Switch);
244
+ MemoizedSwitch.displayName = "Switch";
245
+
246
+ export { MemoizedSwitch as Switch };
package/src/labs/index.ts CHANGED
@@ -27,3 +27,5 @@ export * from "./PaginatedTable";
27
27
 
28
28
  export * from "./GroupPicker";
29
29
  export * from "./VirtualizedAutocomplete";
30
+
31
+ export * from "./Switch";
@@ -1355,6 +1355,62 @@ export const components = ({
1355
1355
  },
1356
1356
  },
1357
1357
  },
1358
+ MuiSwitch: {
1359
+ styleOverrides: {
1360
+ root: {
1361
+ position: "relative",
1362
+ padding: `${odysseyTokens.Spacing0} 0`,
1363
+ width: odysseyTokens.Spacing7,
1364
+ height: odysseyTokens.Spacing5,
1365
+ overflow: "visible",
1366
+ },
1367
+ switchBase: ({ ownerState }) => ({
1368
+ top: odysseyTokens.Spacing1,
1369
+ left: odysseyTokens.Spacing1,
1370
+ padding: `${odysseyTokens.Spacing0} 0`,
1371
+
1372
+ ...(ownerState.checked === true && {
1373
+ color: `${odysseyTokens.HueNeutralWhite} !important`,
1374
+ transform: `translateX(${odysseyTokens.Spacing4}) !important`,
1375
+ }),
1376
+
1377
+ "&:hover": {
1378
+ backgroundColor: "transparent",
1379
+
1380
+ ...(ownerState.checked === true && {
1381
+ backgroundColor: "transparent !important",
1382
+ }),
1383
+ },
1384
+ // Had to use `Mui-focusVisible` class here. `:focus-visible` was not being triggered correctly.
1385
+ "&.Mui-focusVisible": {
1386
+ // Focus styles applied this way due to MUI not applying `Mui-focusVisible` class to the correct element see: https://github.com/mui/material-ui/issues/34986
1387
+ " + .MuiSwitch-track": {
1388
+ boxShadow: `0 0 0 2px ${odysseyTokens.HueNeutralWhite}, 0 0 0 4px ${odysseyTokens.PalettePrimaryMain}`,
1389
+ },
1390
+ },
1391
+ }),
1392
+ thumb: {
1393
+ width: odysseyTokens.Spacing4,
1394
+ height: odysseyTokens.Spacing4,
1395
+ boxShadow: "none",
1396
+ },
1397
+ track: ({ ownerState }) => ({
1398
+ borderRadius: odysseyTokens.BorderRadiusRound,
1399
+ backgroundColor: `${odysseyTokens.HueNeutral300}`,
1400
+ opacity: 1,
1401
+
1402
+ ...(ownerState.checked === true && {
1403
+ // !important used to override more specific .Mui-checked class
1404
+ opacity: "1 !important",
1405
+ backgroundColor: `${odysseyTokens.PaletteSuccessLight} !important`,
1406
+ }),
1407
+ }),
1408
+ input: {
1409
+ height: "44px",
1410
+ top: "-14px",
1411
+ },
1412
+ },
1413
+ },
1358
1414
  MuiDialog: {
1359
1415
  defaultProps: {
1360
1416
  scroll: "paper",