我已经使用包含组件的ThemeUI设置并发布到私人服务器的 StoryBook 设计系统。一个这样的组件是如下所示的按钮。
import React, { FC } from "react";
import { Button as ButtonUI, SxStyleProp } from "theme-ui";
export const Button: FC<ButtonProps> = ({ onClick, children, variant, sx, disabled, onMouseOver, onMouseOut }) => {
return (
<ButtonUI
disabled={disabled || false}
variant={variant || "primary"}
onClick={onClick}
sx={{...sx}}
onMouseOver={onMouseOver || (() => {})}
onMouseOut={onMouseOut || (() => {})}
>
{children}
</ButtonUI>
);
};
export type ButtonProps = {
/**
* The action to perform when the button is clicked
*/
onClick: () => void;
/**
* The contents of the button
*/
children?: any;
/**
* The type of button
*/
variant?: string;
/**
* custom styles for the button
*/
sx?: SxStyleProp;
/**
* If the button is disabled
*/
disabled?: boolean;
/**
* The action to perform if the mouse moves over the button
*/
onMouseOver?: () => void;
/**
* The action to perform if the mouse moves off the button
*/
onMouseOut?: () => void;
};
Button.defaultProps = {
variant: "primary",
disabled: false
};
当我在一个单独的项目中将此组件导入我的 React 应用程序时,该组件会呈现,但 sx 道具中的所有属性都将被忽略......
/** @jsx jsx */
import { Flex, jsx } from "theme-ui";
import Modal from "../Modal";
import { Button } from "<<<PRIVATE SERVER>>>";
/**
* Renders a popup the gives the player the option to logout
* @param title the heading of the modal
* @param confirmString the text for the confirm button
* @param cancelString the text for the cancel button
* @param confirmAction the action to perform when the user confirms
* @param cancelAction the action to perform when the user cancels
*/
export default function LogoutModal({ title, confirmString, cancelString, confirmAction, cancelAction }: Props) {
return (
<Modal
title={title}
closeAction={cancelAction}
children={
<Flex>
<Button
onClick={confirmAction}
sx={{
mr: 1
}}
variant="negative">{confirmString}</Button>
<Button
onClick={cancelAction}
sx={{
ml: 1
}}
variant="secondary">{cancelString}</Button>
</Flex>
}
/>
);
}
interface Props {
title: string;
confirmString: string;
cancelString: string;
confirmAction: () => void;
cancelAction: () => void;
}
所以按钮呈现但没有适用的边距(或我在 sx 道具中添加的任何其他样式。有谁知道为什么会这样?
有趣的是,如果我在 Storybook 中的其他地方调用该组件(而不是在导入单独的项目时), sx 道具会按预期工作。