Skip to content

Instantly share code, notes, and snippets.

@jericbas
Created August 11, 2023 16:43
Show Gist options
  • Save jericbas/c2da2a9528a2933a087c15462390c616 to your computer and use it in GitHub Desktop.
Save jericbas/c2da2a9528a2933a087c15462390c616 to your computer and use it in GitHub Desktop.
Refining "args" type definition - Storybook 7

The current "args" configuration in Storybook for the Button component lacks strict type enforcement, leading to potential inconsistencies. This gist introduces a refined type definition, ensuring accurate prop validation and a more robust development experience.

import { Button } from "./Button";
import { Meta, StoryObj as _StoryObj } from "@storybook/react";
import React from "react";
const meta: Meta<typeof Button> = {
title: "Button",
args: {
children: "Button",
}
};
export default meta;
interface Story extends _StoryObj<typeof Button> {
args: React.ComponentProps<typeof Button>;
}
export const Example: Story = {
args: {
...meta.args,
size: "small"
}
};
export const Example2: Story = {
args: { // Error here if required props is missing
...meta.args,
}
};
type ButtonProps = React.ComponentProps<"button"> & {
size: "small" | "medium" | "large";
};
const sizeToPadding: Record<ButtonProps["size"], string> = {
small: "0.5rem 1rem",
medium: "0.75rem 1.5rem",
large: "1rem 2rem"
};
export function Button({ size, style, ...rest }: ButtonProps) {
return (
<button
{...rest}
style={{
...style,
padding: sizeToPadding[size]
}}
/>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment