Component Playground
By using usePropsEditor
custom hook (available at react-showroom/client
) in your example, you can create a playground for your components (similar to Storybook's Controls/Knobs).
You don't have to manually configure the controls because we extract the props from your props definition and provide sensible default controls.
Simple Example
src/components/button.mdx
Button
NAME | TYPE | DEFAULT | DESCRIPTION |
---|---|---|---|
variant | "primary" | "secondary" | Required | |
fullWidth | boolean | - | |
rounded | "sm" | "lg" | - |
Setting Initial Props
By passing initialProps
options to usePropsEditor
, you can set the initial values for props in the playground.
Note that by default, if typescript able to infer the default value from your code, we will use that. So this should only be used if you do not want the default for your code or a workaround when typescript could not infer the default value.
src/components/button.mdx
Button
NAME | TYPE | DEFAULT | DESCRIPTION |
---|---|---|---|
variant | "primary" | "secondary" | Required | |
fullWidth | boolean | - | |
rounded | "sm" | "lg" | - |
Adding/Overwriting Control
By default, usePropsEditor
will try to infer the control for you based on the props definition. However, we will not infer the props control for the following conditions:
- props that are not supported by our available controls. For instance, you may have a custom object as props, which we could not know how to provide a default control.
- props that comes from
@types/react
. If we include those, it will be a giant list of controls.
Fortunately, you can add those manually by passing controls
options to usePropsEditor
.
src/components/button.mdx
Button
NAME | TYPE | DEFAULT | DESCRIPTION |
---|---|---|---|
variant | "primary" | "secondary" | Required | |
fullWidth | boolean | - | |
rounded | "sm" | "lg" | - |
controls
options is a key-value object where the key is the prop, and the value is the configuration of the control.
Currently, we support the following control types:
type | |
---|---|
text | |
checkbox | |
number | |
select | |
date | |
file | |
object | |
color |
Notes:
- Except for
select
, you can provide the config as string (e.g.'number'
) or as an object (e.g.{ type: 'number' }
). - For
select
, you must provide theoptions
, which is an Array of{ label: string; value: any }
. The value can be anything, including JSX.