[Bug]: @storybook/addon-themes withThemeByClassName类型错误

nle07wnf  于 3个月前  发布在  其他
关注(0)|答案(6)|浏览(52)

描述问题

我在 preview.tsx 文件中使用来自 @storybook/addon-themes 的函数 withThemeByClassName 时遇到了 Typescript 错误,但我认为问题可能出在所有的 Theme 函数上。我正在使用 withThemesByClassName 将 light/dark 模式类传递给 TailwindCSS,以便 TailwindCSS 能够接管并处理组件的 light/dark 显示。虽然这种类型错误似乎不影响添加/更改类的功能,但我还是想提出这个问题。
这是类型问题的完整输出:

Type 'DecoratorFunction<Renderer, Args>' is not assignable to type 'DecoratorFunction<ReactRenderer, { [x: string]: any; }>'.
Types of parameters 'c' and 'c' are incompatible.
Type 'StoryContext<ReactRenderer, { [x: string]: any; }>' is not assignable to type 'StoryContext<Renderer, Args>'.
Types of property 'originalStoryFn' are incompatible.
Type 'StoryFn<ReactRenderer, Args>' is not assignable to type 'StoryFn<Renderer, Args>'.
Type 'LegacyStoryFn<ReactRenderer, Args>' is not assignable to type 'StoryFn<Renderer, Args>'.
Type 'LegacyStoryFn<ReactRenderer, Args>' is not assignable to type 'LegacyStoryFn<Renderer, Args>'.
Types of parameters 'context' and 'context' are incompatible.
                Type 'StoryContext<Renderer, Args>' is not assignable to type 'StoryContext<ReactRenderer, Args>'.
Types of property 'canvasElement' are incompatible.
Type 'unknown' is not assignable to type 'HTMLElement'.ts(2322)
(alias) withThemeByClassName<Renderer>({ themes, defaultTheme, parentSelector, }: ClassNameStrategyConfiguration): DecoratorFunction<Renderer, Args>
import withThemeByClassName

看起来 decorators 需要 DecoratorFunction<ReactRenderer, Args> 而不是 DecoratorFunction<Renderer, Args>
这是一个基于 Next.JS / TailwindCSS 构建的 React 项目。

重现问题

为了通过 NextFont 导入将 Google Fonts 传递给每个故事,我将预览文件转换为 preview.tsx。但我在预览文件中有以下内容。

decorators: [
    withThemeByClassName({
      themes: {
          light: 'light',
          dark: 'dark',
      },
      defaultTheme: 'light',
    }),
    (Story) => (
      <div className={`${montserrat.className}`}>
        <Story />
      </div>
    )
  ]

系统信息

Storybook Environment Info:

  System:
    OS: Windows 10 10.0.19045
    CPU: (8) x64 Intel(R) Core(TM) i7-4790K CPU @ 4.00GHz
  Binaries:
    Node: 20.9.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.19 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
    npm: 10.1.0 - C:\Program Files\nodejs\npm.CMD <----- active
  Browsers:
    Chrome: 120.0.6099.56
    Edge: Spartan (44.19041.3570.0), Chromium (119.0.2151.93)
  npmPackages:
    @storybook/addon-a11y: ^7.6.3 => 7.6.3
    @storybook/addon-essentials: ^7.6.3 => 7.6.3
    @storybook/addon-interactions: ^7.6.3 => 7.6.3
    @storybook/addon-links: ^7.6.3 => 7.6.3
    @storybook/addon-onboarding: ^1.0.8 => 1.0.8
    @storybook/addon-themes: ^7.6.3 => 7.6.3
    @storybook/blocks: ^7.6.3 => 7.6.3
    @storybook/jest: ^0.2.3 => 0.2.3
    @storybook/nextjs: ^7.6.3 => 7.6.3
    @storybook/react: ^7.6.3 => 7.6.3
    @storybook/testing-library: ^0.2.2 => 0.2.2
    eslint-plugin-storybook: ^0.6.15 => 0.6.15
    storybook: ^7.6.3 => 7.6.3
    storybook-dark-mode: ^3.0.3 => 3.0.3

其他相关信息

  • 无响应*
klsxnrf1

klsxnrf11#

让我们尝试将你的Rerender添加到withThemeByClassName中,如下所示:
import { Renderer } from '@storybook/your-rerender'; withThemeByClassName<Renderer>
示例:
import { ReactRenderer } from '@storybook/react';
withThemeByClassName<ReactRenderer>({ themes: { light: 'light', dark: 'dark', }, defaultTheme: 'light', })

yqlxgs2m

yqlxgs2m2#

@pqminh 是的,这解决了类型问题。

jk9hmnmh

jk9hmnmh3#

这对我来说也是一个问题。的确,上述解决方案是有效的。同样的设置:适用于MacOS的Next.js 7.6.5 Storybook,使用TypeScript和TailwindCSS。在withThemeByClassName<ReactRenderer>调用中添加显式类型可以解决这个问题。

goucqfw6

goucqfw64#

你好,Wayne-Jones,
我成功重现了这个问题,并通过查阅文档发现,@pqminh建议的解决方法实际上适用于所有CSS提供者,而不仅仅是Tailwind。因此,我认为问题实际上与语法有关。

n3ipq98p

n3ipq98p5#

@AnandChandrakar 啊,我明白了。我想我刚才是在参考这个页面教程,它直接链接到了设置部分的文档。这个没有那个附加内容: https://storybook.js.org/recipes/tailwindcss

ws51t4hk

ws51t4hk6#

晚上好,Shaun,你能更新一下Recipes页面,使其与addons主题文档中的内容相匹配吗?

相关问题