@sit-onyx/storybook-utils 0.0.0-20250804145452
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/LICENSE.txt +190 -0
- package/README.md +15 -0
- package/package.json +53 -0
- package/src/actions.spec.ts +29 -0
- package/src/actions.ts +188 -0
- package/src/events.ts +777 -0
- package/src/index.ts +5 -0
- package/src/preview.spec.ts +30 -0
- package/src/preview.ts +234 -0
- package/src/required.ts +35 -0
- package/src/sbType.spec.ts +38 -0
- package/src/sbType.ts +104 -0
- package/src/style.css +161 -0
- package/src/theme.ts +127 -0
- package/src/types.ts +11 -0
package/src/theme.ts
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ONYX_BREAKPOINTS as RAW_ONYX_BREAKPOINTS,
|
|
3
|
+
type OnyxBreakpoint,
|
|
4
|
+
} from "@sit-onyx/shared/breakpoints";
|
|
5
|
+
import { create, type ThemeVars } from "storybook/internal/theming";
|
|
6
|
+
import type { Viewport } from "storybook/internal/viewport";
|
|
7
|
+
|
|
8
|
+
export type BrandDetails = Pick<ThemeVars, "brandTitle" | "brandImage" | "brandUrl">;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Get the computed value for a CSS custom property.
|
|
12
|
+
* Per default the property value is taken from the body element.
|
|
13
|
+
*/
|
|
14
|
+
export const getCustomProperty = (property: string, el: Element = document.body) =>
|
|
15
|
+
getComputedStyle(el).getPropertyValue(property);
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Creates a custom theme for Storybook that uses onyx colors.
|
|
19
|
+
*
|
|
20
|
+
* @see https://storybook.js.org/docs/react/configure/theming#create-a-theme-quickstart
|
|
21
|
+
*/
|
|
22
|
+
export const createTheme = (base: "light" | "dark" = "light", brandDetails?: BrandDetails) => {
|
|
23
|
+
const primaryColor = getCustomProperty("--onyx-color-onyx-500");
|
|
24
|
+
return create({
|
|
25
|
+
brandTitle: brandDetails?.brandTitle,
|
|
26
|
+
brandUrl: brandDetails?.brandUrl,
|
|
27
|
+
brandImage: brandDetails?.brandImage,
|
|
28
|
+
brandTarget: "_blank",
|
|
29
|
+
base,
|
|
30
|
+
|
|
31
|
+
// default theme values that are independent of the light/dark mode:
|
|
32
|
+
colorPrimary: primaryColor,
|
|
33
|
+
colorSecondary: getCustomProperty("--onyx-color-themed-secondary-500"),
|
|
34
|
+
barSelectedColor: primaryColor,
|
|
35
|
+
barHoverColor: primaryColor,
|
|
36
|
+
appBorderRadius: remToNumber(getCustomProperty("--onyx-number-radius-300")),
|
|
37
|
+
inputBorderRadius: remToNumber(getCustomProperty("--onyx-number-radius-200")),
|
|
38
|
+
|
|
39
|
+
// custom colors depending on light/dark theme
|
|
40
|
+
...(base === "light" ? getLightTheme() : getDarkTheme()),
|
|
41
|
+
}) satisfies ThemeVars;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
const getLightTheme = (): Partial<ThemeVars> => {
|
|
45
|
+
return defineTheme({
|
|
46
|
+
background: getCustomProperty("--onyx-color-universal-grayscale-white"),
|
|
47
|
+
contentBackground: getCustomProperty("--onyx-color-neutral-steel-100"),
|
|
48
|
+
text: getCustomProperty("--onyx-color-neutral-steel-700"),
|
|
49
|
+
textMuted: getCustomProperty("--onyx-color-neutral-steel-600"),
|
|
50
|
+
border: getCustomProperty("--onyx-color-neutral-steel-300"),
|
|
51
|
+
});
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const getDarkTheme = (): Partial<ThemeVars> => {
|
|
55
|
+
return defineTheme({
|
|
56
|
+
background: getCustomProperty("--onyx-color-neutral-steel-1100"),
|
|
57
|
+
contentBackground: getCustomProperty("--onyx-color-neutral-steel-1200"),
|
|
58
|
+
text: getCustomProperty("--onyx-color-neutral-steel-200"),
|
|
59
|
+
textMuted: getCustomProperty("--onyx-color-neutral-steel-400"),
|
|
60
|
+
border: getCustomProperty("--onyx-color-neutral-steel-900"),
|
|
61
|
+
});
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
/** Define a full onyx Storybook color theme based on the given 5 main colors. */
|
|
65
|
+
const defineTheme = (colors: {
|
|
66
|
+
text: string;
|
|
67
|
+
textMuted: string;
|
|
68
|
+
background: string;
|
|
69
|
+
border: string;
|
|
70
|
+
contentBackground: string;
|
|
71
|
+
}) => {
|
|
72
|
+
return {
|
|
73
|
+
// UI
|
|
74
|
+
appBg: colors.background,
|
|
75
|
+
appContentBg: colors.contentBackground,
|
|
76
|
+
appPreviewBg: colors.contentBackground,
|
|
77
|
+
appBorderColor: colors.border,
|
|
78
|
+
|
|
79
|
+
// Text colors
|
|
80
|
+
textColor: colors.text,
|
|
81
|
+
textInverseColor: colors.contentBackground,
|
|
82
|
+
|
|
83
|
+
// Toolbar default and active/hover colors
|
|
84
|
+
barTextColor: colors.text,
|
|
85
|
+
barBg: colors.background,
|
|
86
|
+
|
|
87
|
+
// Form colors
|
|
88
|
+
inputBg: colors.background,
|
|
89
|
+
inputBorder: colors.border,
|
|
90
|
+
inputTextColor: colors.text,
|
|
91
|
+
booleanBg: colors.background,
|
|
92
|
+
booleanSelectedBg: colors.contentBackground,
|
|
93
|
+
buttonBg: colors.background,
|
|
94
|
+
buttonBorder: colors.border,
|
|
95
|
+
textMutedColor: colors.textMuted,
|
|
96
|
+
} satisfies Partial<ThemeVars>;
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
/** All available Storybook breakpoints / viewports supported by onyx. */
|
|
100
|
+
export const ONYX_BREAKPOINTS = Object.entries(RAW_ONYX_BREAKPOINTS).reduce(
|
|
101
|
+
(obj, [name, width]) => {
|
|
102
|
+
const breakpoint = name as OnyxBreakpoint;
|
|
103
|
+
|
|
104
|
+
const TYPES: Record<OnyxBreakpoint, Viewport["type"]> = {
|
|
105
|
+
"2xs": "mobile",
|
|
106
|
+
xs: "mobile",
|
|
107
|
+
sm: "tablet",
|
|
108
|
+
md: "tablet",
|
|
109
|
+
lg: "desktop",
|
|
110
|
+
xl: "desktop",
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
obj[breakpoint] = {
|
|
114
|
+
name: breakpoint,
|
|
115
|
+
styles: { width: `${width}px`, height: "100%" },
|
|
116
|
+
type: TYPES[breakpoint],
|
|
117
|
+
};
|
|
118
|
+
return obj;
|
|
119
|
+
},
|
|
120
|
+
{} as Record<OnyxBreakpoint, Viewport>,
|
|
121
|
+
);
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Converts a rem string into a numeric value with a rem base of 16.
|
|
125
|
+
* @example "1rem" => 16
|
|
126
|
+
*/
|
|
127
|
+
const remToNumber = (value: string) => +value.replace("rem", "") * 16;
|
package/src/types.ts
ADDED