Skip to content

Commit

Permalink
🏷️ [#445] Convert the Body component and stories to TS
Browse files Browse the repository at this point in the history
Turns out we need the babel presets after all for the storybook
processing :( I'm not sure why things work without in the renderer
repo, but we'll figure it out at some point.

This strongly types the Body component with its 'component' prop,
allowing strings of intrinsic elements to be passed. Support for
custom react components is dropped, as it doesn't appear to be
used anywhere so this simplifies things a bit.
  • Loading branch information
sergei-maertens committed Mar 3, 2025
1 parent 7ab6f23 commit 43e5063
Show file tree
Hide file tree
Showing 8 changed files with 328 additions and 82 deletions.
File renamed without changes.
File renamed without changes.
312 changes: 275 additions & 37 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@
],
"devDependencies": {
"@babel/core": "^7.16.0",
"@babel/preset-react": "^7.26.3",
"@babel/preset-typescript": "^7.26.0",
"@codecov/vite-plugin": "^1.7.0",
"@formatjs/cli": "^6.6.1",
"@gemeente-denhaag/design-tokens-components": "^0.2.3-alpha.315",
Expand Down Expand Up @@ -179,6 +181,10 @@
"formiojs": "npm:@open-formulieren/formiojs@4.13.14"
},
"babel": {
"presets": [
["@babel/preset-react", { "runtime": "automatic" }],
"@babel/preset-typescript"
],
"plugins": [
[
"formatjs",
Expand Down
23 changes: 0 additions & 23 deletions src/components/Body.jsx

This file was deleted.

2 changes: 1 addition & 1 deletion src/components/Body.mdx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ArgTypes, Canvas, Meta, Story} from '@storybook/blocks';
import {ArgTypes, Canvas, Meta} from '@storybook/blocks';

import * as BodyStories from './Body.stories';

Expand Down
40 changes: 19 additions & 21 deletions src/components/Body.stories.jsx → src/components/Body.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import type {Meta, StoryObj} from '@storybook/react';

import Body, {VARIANTS} from './Body';

export default {
title: 'Pure React components / Body',
component: Body,
args: {
children: 'Body',
modifiers: [],
},
argTypes: {
children: {table: {disable: true}},
component: {control: {disable: true}},
Expand All @@ -16,35 +22,27 @@ export default {
parameters: {
controls: {sort: 'requiredFirst'},
},
};
} satisfies Meta<typeof Body>;

const render = ({content = 'Body', modifiers = []}) => {
const isWysiwyg = modifiers.includes('wysiwyg');
const extra = isWysiwyg
? {
dangerouslySetInnerHTML: {__html: content},
component: 'div',
}
: {};
return (
<Body modifiers={modifiers} {...extra}>
{isWysiwyg ? null : content}
</Body>
);
};
type Story = StoryObj<typeof Body>;

export const Default = {
render,
export const Default: Story = {
args: {
content: 'Lorem ipsum...',
children: 'Lorem ipsum...',
modifiers: [],
},
};

export const WYSIWYG = {
render,
export const WYSIWYG: Story = {
render: ({children, modifiers}) => (
<Body
modifiers={modifiers}
component="div"
dangerouslySetInnerHTML={{__html: children as string}}
/>
),
args: {
content:
children:
'<p>Lorem ipsum with a <a href="https://example.com" target="_blank" rel="noopener nofollower">clickable</a> link...<p>',
modifiers: ['wysiwyg'],
},
Expand Down
27 changes: 27 additions & 0 deletions src/components/Body.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {getBEMClassName} from 'utils';

export const VARIANTS = ['big', 'muted', 'small', 'wysiwyg', 'inline'] as const;
export type Variant = (typeof VARIANTS)[number];

export type BodyProps<T extends React.ElementType = 'p'> = {
children?: React.ReactNode;
component?: T;
modifiers?: Variant[];
} & React.ComponentPropsWithoutRef<T>;

const Body = <T extends React.ElementType = 'p'>({
modifiers = [],
component,
children,
...props
}: BodyProps<T>) => {
const Component = component || 'p';
const className = getBEMClassName('body', modifiers);
return (
<Component className={className} {...props}>
{children}
</Component>
);
};

export default Body;

0 comments on commit 43e5063

Please sign in to comment.