@okta/odyssey-react-mui 1.7.0 → 1.7.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.
- package/CHANGELOG.md +4 -0
- package/dist/Badge.js +73 -0
- package/dist/Badge.js.map +1 -0
- package/dist/OdysseyDesignTokensContext.js.map +1 -1
- package/dist/src/Badge.d.ts +22 -0
- package/dist/src/Badge.d.ts.map +1 -0
- package/dist/src/OdysseyDesignTokensContext.d.ts +1 -0
- package/dist/src/OdysseyDesignTokensContext.d.ts.map +1 -1
- package/dist/tsconfig.production.tsbuildinfo +1 -1
- package/package.json +3 -3
- package/src/Badge.tsx +99 -0
- package/src/OdysseyDesignTokensContext.tsx +2 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@okta/odyssey-react-mui",
|
|
3
|
-
"version": "1.7.
|
|
3
|
+
"version": "1.7.1",
|
|
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.7.
|
|
54
|
+
"@okta/odyssey-design-tokens": "1.7.1",
|
|
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": "
|
|
66
|
+
"gitHead": "ba092e8f00431e0280b372b95da29a0345afede6"
|
|
67
67
|
}
|
package/src/Badge.tsx
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Copyright (c) 2023-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 { CSSProperties, memo, useMemo } from "react";
|
|
14
|
+
|
|
15
|
+
import {
|
|
16
|
+
useOdysseyDesignTokens,
|
|
17
|
+
DesignTokens,
|
|
18
|
+
} from "./OdysseyDesignTokensContext";
|
|
19
|
+
import { Box } from "./Box";
|
|
20
|
+
import type { SeleniumProps } from "./SeleniumProps";
|
|
21
|
+
|
|
22
|
+
export const badgeTypeValues = ["default", "attention", "danger"] as const;
|
|
23
|
+
|
|
24
|
+
export type BadgeProps = {
|
|
25
|
+
badgeContent: number;
|
|
26
|
+
badgeContentMax?: number;
|
|
27
|
+
type?: (typeof badgeTypeValues)[number];
|
|
28
|
+
} & SeleniumProps;
|
|
29
|
+
|
|
30
|
+
const badgeTypeColors = (odysseyTokens: DesignTokens) => ({
|
|
31
|
+
default: {
|
|
32
|
+
background: odysseyTokens.HueNeutral200,
|
|
33
|
+
font: odysseyTokens.TypographyColorBody,
|
|
34
|
+
},
|
|
35
|
+
attention: {
|
|
36
|
+
background: odysseyTokens.PalettePrimaryMain,
|
|
37
|
+
font: odysseyTokens.TypographyColorInverse,
|
|
38
|
+
},
|
|
39
|
+
danger: {
|
|
40
|
+
background: odysseyTokens.PaletteDangerMain,
|
|
41
|
+
font: odysseyTokens.TypographyColorInverse,
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
const Badge = ({
|
|
46
|
+
badgeContent,
|
|
47
|
+
badgeContentMax = 999,
|
|
48
|
+
type = "default",
|
|
49
|
+
}: BadgeProps) => {
|
|
50
|
+
const odysseyDesignTokens = useOdysseyDesignTokens();
|
|
51
|
+
|
|
52
|
+
const greaterThanZeroContentMax = badgeContentMax > 0 ? badgeContentMax : 1;
|
|
53
|
+
const threeDigitLimitedMax =
|
|
54
|
+
greaterThanZeroContentMax > 999 ? 999 : greaterThanZeroContentMax;
|
|
55
|
+
const isOverContentMax = Boolean(
|
|
56
|
+
badgeContent && badgeContent > threeDigitLimitedMax
|
|
57
|
+
);
|
|
58
|
+
const overContentMaxMessage = `${greaterThanZeroContentMax}+`;
|
|
59
|
+
const formattedContent = isOverContentMax
|
|
60
|
+
? overContentMaxMessage
|
|
61
|
+
: badgeContent;
|
|
62
|
+
const contentIsLongerThanOneChar = formattedContent?.toString()?.length > 1;
|
|
63
|
+
|
|
64
|
+
const badgeStyles = useMemo<CSSProperties>(
|
|
65
|
+
() => ({
|
|
66
|
+
display: "inline-flex",
|
|
67
|
+
alignItems: "center",
|
|
68
|
+
justifyContent: "center",
|
|
69
|
+
minWidth: `calc(${odysseyDesignTokens.Spacing4} + ${odysseyDesignTokens.Spacing1})`,
|
|
70
|
+
height: `calc(${odysseyDesignTokens.Spacing4} + ${odysseyDesignTokens.Spacing1})`,
|
|
71
|
+
minHeight: `calc(${odysseyDesignTokens.Spacing4} + ${odysseyDesignTokens.Spacing1})`,
|
|
72
|
+
// 6px horizontal padding per design requirements
|
|
73
|
+
padding: `0 calc(${odysseyDesignTokens.Spacing1} * 1.5)`,
|
|
74
|
+
backgroundColor: badgeTypeColors(odysseyDesignTokens)[type].background,
|
|
75
|
+
color: badgeTypeColors(odysseyDesignTokens)[type].font,
|
|
76
|
+
borderRadius: contentIsLongerThanOneChar
|
|
77
|
+
? `${odysseyDesignTokens.BorderRadiusOuter}`
|
|
78
|
+
: "50%",
|
|
79
|
+
fontSize: `${odysseyDesignTokens.TypographyScale0}`,
|
|
80
|
+
fontFamily: `${odysseyDesignTokens.TypographyFamilyMono}`,
|
|
81
|
+
fontWeight: `${odysseyDesignTokens.TypographyWeightBodyBold}`,
|
|
82
|
+
lineHeight: 1,
|
|
83
|
+
}),
|
|
84
|
+
[type, contentIsLongerThanOneChar, odysseyDesignTokens]
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
const shouldHideBadge = badgeContent <= 0 || !badgeContent;
|
|
88
|
+
|
|
89
|
+
if (shouldHideBadge) {
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return <Box sx={badgeStyles}>{formattedContent}</Box>;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
const MemoizedBadge = memo(Badge);
|
|
97
|
+
MemoizedBadge.displayName = "Badge";
|
|
98
|
+
|
|
99
|
+
export { MemoizedBadge as Badge };
|
|
@@ -13,7 +13,8 @@
|
|
|
13
13
|
import * as Tokens from "@okta/odyssey-design-tokens";
|
|
14
14
|
import { createContext, useContext } from "react";
|
|
15
15
|
|
|
16
|
-
export
|
|
16
|
+
export type DesignTokens = typeof Tokens;
|
|
17
|
+
export const OdysseyDesignTokensContext = createContext<DesignTokens>(Tokens);
|
|
17
18
|
|
|
18
19
|
export const useOdysseyDesignTokens = () =>
|
|
19
20
|
useContext(OdysseyDesignTokensContext);
|