Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Theme Next - accent switch #3395

Merged
merged 2 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .changeset/salt-provider-next-accent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
"@salt-ds/core": minor
---

Added a new `accent` prop to `UNSTABLE_SaltProviderNext` with `"blue"` or `"teal"` option. To try it out, use

```
<UNSTABLE_SaltProviderNext accent="teal">
```

Refer to [documentation](https://storybook.saltdesignsystem.com/?path=/docs/experimental-theme-next--docs) for more information.
2 changes: 2 additions & 0 deletions .changeset/strong-parents-march.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
Switched to use new color palette in theme next when using `UNSTABLE_SaltProviderNext`.

Refer to [documentation](https://storybook.saltdesignsystem.com/?path=/docs/experimental-theme-next--docs) for more information.

Closes #3394
10 changes: 10 additions & 0 deletions .storybook/preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,16 @@ export const globalTypes: GlobalTypes = {
title: "Heading font",
},
},
accent: {
name: "Experimental accent",
description: "Switch accent to blue / teal",
defaultValue: "blue",
toolbar: {
icon: "beaker",
items: ["blue", "teal"],
title: "Accent",
},
},
};

export const argTypes: ArgTypes = {
Expand Down
13 changes: 11 additions & 2 deletions docs/decorators/withTheme.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,21 @@
theme,
"text",
"primary-foreground",
document.querySelector(".salt-theme") as HTMLElement

Check failure on line 43 in docs/decorators/withTheme.tsx

View workflow job for this annotation

GitHub Actions / lint

Use a ! assertion to more succinctly remove null and undefined from the type
);
const background = getCharacteristicValue(
theme,
"container",
"primary-background",
document.querySelector(".salt-theme") as HTMLElement

Check failure on line 49 in docs/decorators/withTheme.tsx

View workflow job for this annotation

GitHub Actions / lint

Use a ! assertion to more succinctly remove null and undefined from the type
);

addBackgroundStyle(
selectorId,
`
${selector} {
background: ${background || "unset"};

Check failure on line 56 in docs/decorators/withTheme.tsx

View workflow job for this annotation

GitHub Actions / lint

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator
color: ${color || "unset"};

Check failure on line 57 in docs/decorators/withTheme.tsx

View workflow job for this annotation

GitHub Actions / lint

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator
transition: background-color 0.3s;
}
`
Expand All @@ -65,8 +65,15 @@
}

export const withTheme: Decorator = (StoryFn, context) => {
const { density, mode, styleInjection, themeNext, corner, headingFont } =
context.globals;
const {
density,
mode,
styleInjection,
themeNext,
corner,
headingFont,
accent,
} = context.globals;

const Provider =
themeNext === "enable" ? UNSTABLE_SaltProviderNext : SaltProvider;
Expand All @@ -91,12 +98,13 @@
{ModeValues.map((mode) => (
<Provider
applyClassesTo={"child"}
density={density}

Check failure on line 101 in docs/decorators/withTheme.tsx

View workflow job for this annotation

GitHub Actions / lint

Unsafe assignment of an `any` value
mode={mode}
key={`${mode}-${styleInjection}`}
enableStyleInjection={styleInjection === "enable"}
corner={corner}

Check failure on line 105 in docs/decorators/withTheme.tsx

View workflow job for this annotation

GitHub Actions / lint

Unsafe assignment of an `any` value
headingFont={headingFont}

Check failure on line 106 in docs/decorators/withTheme.tsx

View workflow job for this annotation

GitHub Actions / lint

Unsafe assignment of an `any` value
accent={accent}
>
<Panel>
<StoryFn />
Expand All @@ -115,6 +123,7 @@
enableStyleInjection={styleInjection === "enable"}
corner={corner}
headingFont={headingFont}
accent={accent}
>
<SetBackground viewMode={context.viewMode} id={context.id} />
<StoryFn />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ const TestComponent = ({
className?: string;
}) => {
const density = useDensity();
const { theme, mode, UNSTABLE_corner, themeNext } = useTheme();
const { theme, mode, UNSTABLE_corner, UNSTABLE_accent, themeNext } =
useTheme();
const { announce } = useAriaAnnouncer();
const announcerPresent = typeof announce === "function";

Expand All @@ -32,6 +33,7 @@ const TestComponent = ({
data-mode={mode}
data-announcer={announcerPresent}
data-corner={UNSTABLE_corner}
data-accent={UNSTABLE_accent}
data-themeNext={themeNext}
/>
);
Expand Down Expand Up @@ -274,6 +276,7 @@ describe("Given a SaltProviderNext", () => {
.should("exist")
.and("have.attr", "data-mode", "light")
.and("have.attr", "data-corner", "sharp")
.and("have.attr", "data-accent", "blue")
.and("have.class", "salt-theme-next")
.and("have.class", "salt-density-medium");
});
Expand All @@ -289,6 +292,7 @@ describe("Given a SaltProviderNext", () => {
.and("have.attr", "data-mode", "light")
.and("have.attr", "data-announcer", "true")
.and("have.attr", "data-corner", "sharp")
.and("have.attr", "data-accent", "blue")
.and("have.attr", "data-themeNext", "true");
cy.get("[aria-live]").should("exist");
});
Expand All @@ -297,7 +301,12 @@ describe("Given a SaltProviderNext", () => {
describe("when nested", () => {
it("should inherit values not passed as props", () => {
mount(
<UNSTABLE_SaltProviderNext density="high" mode="dark" corner="rounded">
<UNSTABLE_SaltProviderNext
density="high"
mode="dark"
corner="rounded"
accent="teal"
>
<TestComponent />
<UNSTABLE_SaltProviderNext density="medium">
<TestComponent id="test-2" />
Expand All @@ -313,20 +322,31 @@ describe("Given a SaltProviderNext", () => {
.and("have.attr", "data-density", "high")
.and("have.attr", "data-mode", "dark")
.and("have.attr", "data-corner", "rounded")
.and("have.attr", "data-accent", "teal")
.and("have.attr", "data-announcer", "true");

cy.get("#test-2")
.should("exist")
.and("have.attr", "data-density", "medium")
.and("have.attr", "data-mode", "dark")
.and("have.attr", "data-corner", "rounded")
.and("have.attr", "data-accent", "teal")
.and("have.attr", "data-announcer", "true");
});
it("should take different values set as props", () => {
mount(
<UNSTABLE_SaltProviderNext density="high" mode="dark" corner="rounded">
<UNSTABLE_SaltProviderNext
density="high"
mode="dark"
corner="rounded"
accent="teal"
>
<TestComponent />
<UNSTABLE_SaltProviderNext density="medium" corner="sharp">
<UNSTABLE_SaltProviderNext
density="medium"
corner="sharp"
accent="blue"
>
<TestComponent id="test-2" />
</UNSTABLE_SaltProviderNext>
</UNSTABLE_SaltProviderNext>
Expand All @@ -340,13 +360,15 @@ describe("Given a SaltProviderNext", () => {
.and("have.attr", "data-density", "high")
.and("have.attr", "data-mode", "dark")
.and("have.attr", "data-corner", "rounded")
.and("have.attr", "data-accent", "teal")
.and("have.attr", "data-announcer", "true");

cy.get("#test-2")
.should("exist")
.and("have.attr", "data-density", "medium")
.and("have.attr", "data-mode", "dark")
.and("have.attr", "data-corner", "sharp")
.and("have.attr", "data-accent", "blue")
.and("have.attr", "data-announcer", "true");
});
});
Expand Down
17 changes: 16 additions & 1 deletion packages/core/src/salt-provider/SaltProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
} from "@salt-ds/styles";
import { UNSTABLE_Corner } from "../theme/Corner";
import { UNSTABLE_HeadingFont } from "../theme/HeadingFont";
import { UNSTABLE_Accent } from "../theme/Accent";

export const DEFAULT_DENSITY = "medium";

Expand All @@ -35,6 +36,7 @@ const UNSTABLE_ADDITIONAL_THEME_NAME = "salt-theme-next";
const DEFAULT_MODE = "light";
const DEFAULT_CORNER: UNSTABLE_Corner = "sharp";
const DEFAULT_HEADING_FONT: UNSTABLE_HeadingFont = "Open Sans";
const DEFAULT_ACCENT: UNSTABLE_Accent = "blue";
export interface ThemeContextProps {
theme: ThemeName;
mode: Mode;
Expand All @@ -43,6 +45,7 @@ export interface ThemeContextProps {
themeNext: boolean;
UNSTABLE_corner: UNSTABLE_Corner;
UNSTABLE_headingFont: UNSTABLE_HeadingFont;
UNSTABLE_accent: UNSTABLE_Accent;
}

export const DensityContext = createContext<Density>(DEFAULT_DENSITY);
Expand All @@ -53,6 +56,7 @@ export const ThemeContext = createContext<ThemeContextProps>({
themeNext: false,
UNSTABLE_corner: DEFAULT_CORNER,
UNSTABLE_headingFont: DEFAULT_HEADING_FONT,
UNSTABLE_accent: DEFAULT_ACCENT,
});

export const BreakpointContext =
Expand Down Expand Up @@ -88,6 +92,7 @@ const createThemedChildren = ({
themeNext,
corner,
headingFont,
accent,
}: {
children: ReactNode;
themeName: ThemeName;
Expand All @@ -100,6 +105,7 @@ const createThemedChildren = ({
const themeNextProps = {
"data-corner": corner,
"data-heading-font": headingFont,
"data-accent": accent,
};
if (applyClassesTo === "root") {
return children;
Expand Down Expand Up @@ -181,6 +187,7 @@ function InternalSaltProvider({
themeNext,
corner: cornerProp,
headingFont: headingFontProp,
accent: accentProp,
}: Omit<
SaltProviderProps & ThemeNextProps & UNSTABLE_SaltProviderNextProps,
"enableStyleInjection"
Expand All @@ -192,6 +199,7 @@ function InternalSaltProvider({
window: inheritedWindow,
UNSTABLE_corner: inheritedCorner,
UNSTABLE_headingFont: inheritedHeadingFont,
UNSTABLE_accent: inheritedAccent,
} = useContext(ThemeContext);

const isRootProvider = inheritedTheme === undefined || inheritedTheme === "";
Expand All @@ -203,6 +211,7 @@ function InternalSaltProvider({
const corner = cornerProp ?? inheritedCorner ?? DEFAULT_CORNER;
const headingFont =
headingFontProp ?? inheritedHeadingFont ?? DEFAULT_HEADING_FONT;
const accent = accentProp ?? inheritedAccent ?? DEFAULT_ACCENT;

const applyClassesTo =
applyClassesToProp ?? (isRootProvider ? "root" : "scope");
Expand All @@ -222,8 +231,9 @@ function InternalSaltProvider({
themeNext: Boolean(themeNext),
UNSTABLE_corner: corner,
UNSTABLE_headingFont: headingFont,
UNSTABLE_accent: accent,
}),
[themeName, mode, targetWindow, themeNext, corner, headingFont]
[themeName, mode, targetWindow, themeNext, corner, headingFont, accent]
);

const themedChildren = createThemedChildren({
Expand All @@ -235,6 +245,7 @@ function InternalSaltProvider({
themeNext,
corner: corner,
headingFont,
accent,
});

useIsomorphicLayoutEffect(() => {
Expand All @@ -252,6 +263,7 @@ function InternalSaltProvider({
targetWindow.document.documentElement.dataset.corner = corner;
targetWindow.document.documentElement.dataset.headingFont =
headingFont;
targetWindow.document.documentElement.dataset.accent = accent;
}
} else {
console.warn(
Expand All @@ -270,6 +282,7 @@ function InternalSaltProvider({
if (themeNext) {
delete targetWindow.document.documentElement.dataset.corner;
delete targetWindow.document.documentElement.dataset.headingFont;
delete targetWindow.document.documentElement.dataset.accent;
}
}
};
Expand All @@ -283,6 +296,7 @@ function InternalSaltProvider({
themeNext,
corner,
headingFont,
accent,
]);

const matchedBreakpoints = useMatchedBreakpoints(breakpoints);
Expand Down Expand Up @@ -320,6 +334,7 @@ export function SaltProvider({
interface UNSTABLE_SaltProviderNextAdditionalProps {
corner?: UNSTABLE_Corner;
headingFont?: UNSTABLE_HeadingFont;
accent?: UNSTABLE_Accent;
}

export type UNSTABLE_SaltProviderNextProps = SaltProviderProps &
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/theme/Accent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const UNSTABLE_AccentValues = ["blue", "teal"] as const;

export type UNSTABLE_Accent = (typeof UNSTABLE_AccentValues)[number];
1 change: 1 addition & 0 deletions packages/core/src/theme/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./Accent";
export * from "./Density";
export * from "./HeadingFont";
export * from "./Theme";
Expand Down
Loading
Loading