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

Addon Themes: Deprecate useThemeParameters #30111

Merged
merged 1 commit into from
Dec 20, 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
7 changes: 5 additions & 2 deletions code/addons/themes/docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ export const myCustomDecorator =

### `useThemeParameters`

(⛔️ **Deprecated**)
_Do not use this hook anymore. Access the theme directly via the context instead e.g. `context.parameters.themes`_

Returns the theme parameters for this addon.

```js
Expand Down Expand Up @@ -152,14 +155,14 @@ Let's use Vuetify as an example. Vuetify uses it's own global state to know whic
import { DecoratorHelpers } from '@storybook/addon-themes';
import { useTheme } from 'vuetify';

const { initializeThemeState, pluckThemeFromContext, useThemeParameters } = DecoratorHelpers;
const { initializeThemeState, pluckThemeFromContext } = DecoratorHelpers;

export const withVuetifyTheme = ({ themes, defaultTheme }) => {
initializeThemeState(Object.keys(themes), defaultTheme);

return (story, context) => {
const selectedTheme = pluckThemeFromContext(context);
const { themeOverride } = useThemeParameters();
const { themeOverride } = context.parameters.themes ?? {};

const selected = themeOverride || selectedTheme || defaultTheme;

Expand Down
5 changes: 3 additions & 2 deletions code/addons/themes/src/decorators/class-name.decorator.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useEffect } from 'storybook/internal/preview-api';
import type { DecoratorFunction, Renderer } from 'storybook/internal/types';

import { initializeThemeState, pluckThemeFromContext, useThemeParameters } from './helpers';
import { PARAM_KEY } from '../constants';
import { initializeThemeState, pluckThemeFromContext } from './helpers';

export interface ClassNameStrategyConfiguration {
themes: Record<string, string>;
Expand All @@ -22,7 +23,7 @@ export const withThemeByClassName = <TRenderer extends Renderer = Renderer>({
initializeThemeState(Object.keys(themes), defaultTheme);

return (storyFn, context) => {
const { themeOverride } = useThemeParameters(context);
const { themeOverride } = context.parameters[PARAM_KEY] ?? {};
const selected = pluckThemeFromContext(context);

useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useEffect } from 'storybook/internal/preview-api';
import type { DecoratorFunction, Renderer } from 'storybook/internal/types';

import { initializeThemeState, pluckThemeFromContext, useThemeParameters } from './helpers';
import { PARAM_KEY } from '../constants';
import { initializeThemeState, pluckThemeFromContext } from './helpers';

export interface DataAttributeStrategyConfiguration {
themes: Record<string, string>;
Expand All @@ -22,7 +23,7 @@ export const withThemeByDataAttribute = <TRenderer extends Renderer = any>({
}: DataAttributeStrategyConfiguration): DecoratorFunction<TRenderer> => {
initializeThemeState(Object.keys(themes), defaultTheme);
return (storyFn, context) => {
const { themeOverride } = useThemeParameters(context);
const { themeOverride } = context.parameters[PARAM_KEY] ?? {};
const selected = pluckThemeFromContext(context);

useEffect(() => {
Expand Down
19 changes: 16 additions & 3 deletions code/addons/themes/src/decorators/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { addons } from 'storybook/internal/preview-api';
import { deprecate } from 'storybook/internal/client-logger';
import { addons, useParameter } from 'storybook/internal/preview-api';
import type { StoryContext } from 'storybook/internal/types';

import dedent from 'ts-dedent';

import type { ThemeParameters } from '../constants';
import { DEFAULT_THEME_PARAMETERS, GLOBAL_KEY, PARAM_KEY, THEMING_EVENTS } from '../constants';

Expand All @@ -12,8 +15,18 @@ export function pluckThemeFromContext({ globals }: StoryContext): string {
return globals[GLOBAL_KEY] || '';
}

export function useThemeParameters(context: StoryContext): ThemeParameters {
return context.parameters[PARAM_KEY] || DEFAULT_THEME_PARAMETERS;
export function useThemeParameters(context?: StoryContext): ThemeParameters {
deprecate(
dedent`The useThemeParameters function is deprecated. Please access parameters via the context directly instead e.g.
- const { themeOverride } = context.parameters.themes ?? {};
`
);

if (!context) {
return useParameter<ThemeParameters>(PARAM_KEY, DEFAULT_THEME_PARAMETERS) as ThemeParameters;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: casting to ThemeParameters may hide type errors - consider using type guards instead


return context.parameters[PARAM_KEY] ?? DEFAULT_THEME_PARAMETERS;
}

export function initializeThemeState(themeNames: string[], defaultTheme: string) {
Expand Down
6 changes: 4 additions & 2 deletions code/addons/themes/src/decorators/provider.decorator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import React from 'react';
import { useMemo } from 'storybook/internal/preview-api';
import type { DecoratorFunction, Renderer } from 'storybook/internal/types';

import { initializeThemeState, pluckThemeFromContext, useThemeParameters } from './helpers';
import { PARAM_KEY } from '../constants';
import { initializeThemeState, pluckThemeFromContext } from './helpers';

type Theme = Record<string, any>;
type ThemeMap = Record<string, Theme>;
Expand Down Expand Up @@ -32,7 +33,8 @@ export const withThemeFromJSXProvider = <TRenderer extends Renderer = any>({

// eslint-disable-next-line react/display-name
return (storyFn, context) => {
const { themeOverride } = useThemeParameters(context);
// eslint-disable-next-line react/destructuring-assignment
const { themeOverride } = context.parameters[PARAM_KEY] ?? {};
const selected = pluckThemeFromContext(context);

const theme = useMemo(() => {
Expand Down
Loading