-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify theme next storybook toolbar (#3393)
- Loading branch information
Showing
6 changed files
with
719 additions
and
280 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,16 @@ | ||
import { addons } from "@storybook/manager-api"; | ||
import { addons, types } from "@storybook/manager-api"; | ||
import saltTheme from "./SaltTheme"; | ||
import { ThemeNextToolbar } from "./toolbar/ThemeNextToolbar"; | ||
|
||
addons.setConfig({ | ||
theme: saltTheme, | ||
}); | ||
|
||
addons.register("theme-next-addon", () => { | ||
addons.add("theme-next-addon/toolbar", { | ||
title: "Theme next toolbar", | ||
//👇 Sets the type of UI element in Storybook | ||
type: types.TOOL, | ||
render: ThemeNextToolbar, | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* Custom Toolbar */ | ||
.theme-next-toolbar-group-wrapper { | ||
cursor: not-allowed; | ||
} | ||
|
||
.theme-next-toolbar-group-wrapper > span > span { | ||
font-weight: bold; | ||
color: darkgray; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import type { TooltipLinkListLink } from "@storybook/components"; | ||
import { | ||
IconButton, | ||
Separator, | ||
TooltipLinkList, | ||
WithTooltip, | ||
} from "@storybook/components"; | ||
import { BeakerIcon, CheckIcon } from "@storybook/icons"; | ||
import { useGlobals } from "@storybook/manager-api"; | ||
import { clsx } from "clsx"; | ||
import React, { AnchorHTMLAttributes } from "react"; | ||
|
||
import "./ThemeNextToolbar.css"; | ||
|
||
const description = "Theme next controls"; | ||
|
||
const camelCaseToWords = (s: string) => { | ||
const result = s.replace(/([A-Z])/g, " $1"); | ||
return result.charAt(0).toUpperCase() + result.slice(1); | ||
}; | ||
|
||
export const globalOptions: Record< | ||
string, | ||
{ name: string; description: string; defaultValue: string; items: string[] } | ||
> = { | ||
themeNext: { | ||
name: "Experimental theme next", | ||
description: "Turn on/off theme next", | ||
defaultValue: "disable", | ||
items: ["enable", "disable"], | ||
}, | ||
corner: { | ||
name: "Experimental corner", | ||
description: "Switch corner to sharp / rounded", | ||
defaultValue: "sharp", | ||
items: ["sharp", "rounded"], | ||
}, | ||
headingFont: { | ||
name: "Experimental heading font", | ||
description: "Switch heading font to open sans / amplitude", | ||
defaultValue: "Open Sans", | ||
items: ["Open Sans", "Amplitude"], | ||
}, | ||
accent: { | ||
name: "Experimental accent", | ||
description: "Switch accent to blue / teal", | ||
defaultValue: "blue", | ||
items: ["blue", "teal"], | ||
}, | ||
actionFont: { | ||
name: "Experimental action font", | ||
description: "Switch action font to open sans / amplitude", | ||
defaultValue: "Open Sans", | ||
items: ["Open Sans", "Amplitude"], | ||
}, | ||
}; | ||
|
||
const GroupWrapper = ({ | ||
className, | ||
children, | ||
}: AnchorHTMLAttributes<HTMLAnchorElement>) => { | ||
return ( | ||
<div | ||
className={clsx(className, "theme-next-toolbar-group-wrapper")} | ||
children={children} | ||
/> | ||
); | ||
}; | ||
|
||
export const ThemeNextToolbar = ({ active }: { active?: boolean }) => { | ||
const [globals, updateGlobals] = useGlobals(); | ||
|
||
const items: TooltipLinkListLink[] = Object.keys(globalOptions).flatMap( | ||
(globalKey) => { | ||
return [ | ||
{ | ||
id: `theme-next-${globalKey}-header`, | ||
title: camelCaseToWords(globalKey), | ||
LinkWrapper: GroupWrapper, // Custom wrapper to render group | ||
href: "#", // Without href, `LinkWrapper` will not work | ||
}, | ||
...globalOptions[globalKey].items.map((value) => { | ||
const disabled = | ||
globalKey === "themeNext" | ||
? false | ||
: globals["themeNext"] !== "enable"; | ||
const active = globals[globalKey] === value; | ||
|
||
return { | ||
id: `theme-next-${globalKey}-${value}`, | ||
right: active ? ( | ||
<CheckIcon style={{ fill: "inherit" }} /> | ||
) : undefined, | ||
active, | ||
title: camelCaseToWords(value), | ||
onClick: () => { | ||
!disabled && updateGlobals({ [globalKey]: value }); | ||
}, | ||
disabled, | ||
}; | ||
}), | ||
]; | ||
} | ||
); | ||
|
||
return ( | ||
<> | ||
<Separator /> | ||
<WithTooltip | ||
tooltip={() => <TooltipLinkList links={items} />} | ||
trigger="click" | ||
closeOnOutsideClick | ||
> | ||
<IconButton title={description} active={active}> | ||
<BeakerIcon /> Theme Next | ||
</IconButton> | ||
</WithTooltip> | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.