Skip to content

Commit 7c4e4ef

Browse files
committed
Improve the documentation of the alert
1 parent 1b904da commit 7c4e4ef

File tree

4 files changed

+64
-12
lines changed

4 files changed

+64
-12
lines changed

docs/Alert.stories.tsx

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,51 @@
11
import { Alert } from "../dist/Alert";
22
import type { AlertProps } from "../dist/Alert";
33
import { sectionName } from "./sectionName";
4-
import { getStoryFactory } from "./getStory";
4+
import { getStoryFactory, logCallbacks } from "./getStory";
55
import { assert } from "tsafe/assert";
66
import type { Equals } from "tsafe";
77

8-
const severities = ["success", "warning", "info", "error"] as const;
9-
10-
assert<Equals<typeof severities[number], AlertProps["severity"]>>();
11-
128
const { meta, getStory } = getStoryFactory({
139
sectionName,
1410
"wrappedComponent": { Alert },
1511
"argTypes": {
1612
"severity": {
17-
"options": severities,
13+
"options": (() => {
14+
const severities = ["success", "warning", "info", "error"] as const;
15+
16+
assert<Equals<typeof severities[number], AlertProps["severity"]>>();
17+
18+
return severities;
19+
})(),
1820
"control": { "type": "radio" }
21+
},
22+
"isClosable": {
23+
"description": "If the modal should have a close button"
24+
},
25+
"onClose": {
26+
"description": "Called when the user clicks the close button"
27+
},
28+
"isClosed": {
29+
"description": [
30+
"If specified the `<Alert />` is in [controlled mode](https://reactjs.org/docs/forms.html#controlled-components)",
31+
"this means that when the close button is clicked",
32+
"the `onClose()` callback will be called but you are responsible",
33+
"for setting `isClosed` to `false`, the `<Alert />` wont close itself."
34+
].join(" "),
35+
"control": { "type": null }
1936
}
2037
}
2138
});
2239

2340
export default meta;
2441

25-
export const Success = getStory({
42+
export const Default = getStory({
2643
"severity": "success",
2744
"title": "Message successfully sent",
28-
"description": "Everything went well"
45+
"description": "Everything went well",
46+
"isClosable": true,
47+
"isClosed": undefined,
48+
...logCallbacks(["onClose"])
2949
});
3050

3151
export const Small = getStory({

docs/getStory.tsx

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,16 @@ export function getStoryFactory<Props extends Record<string, any>>(params: {
2323
Props & {
2424
darkMode: boolean;
2525
containerWidth: number;
26+
isFirstStory: boolean;
2627
}
27-
> = ({ darkMode, containerWidth, ...props }) => {
28+
> = ({ darkMode, containerWidth, isFirstStory, ...props }) => {
2829
const { setIsDark } = useIsDark();
2930

3031
useEffect(() => {
32+
if (!isFirstStory) {
33+
return;
34+
}
35+
3136
setIsDark(darkMode);
3237
}, [darkMode]);
3338

@@ -40,29 +45,54 @@ export function getStoryFactory<Props extends Record<string, any>>(params: {
4045
);
4146
};
4247

48+
let isFirstStory = true;
49+
4350
function getStory(props: Props): typeof Template {
4451
const out = Template.bind({});
4552

4653
out.args = {
4754
"darkMode": window.matchMedia("(prefers-color-scheme: dark)").matches,
4855
"containerWidth": defaultContainerWidth ?? 0,
56+
isFirstStory,
4957
...props
5058
};
5159

60+
isFirstStory = false;
61+
62+
out.parameters = {
63+
docs: {
64+
description: {
65+
story: "Some story **markdown**"
66+
}
67+
}
68+
};
69+
5270
return out;
5371
}
5472

5573
return {
5674
"meta": id<Meta>({
5775
"title": `${sectionName}/${symToStr(wrappedComponent)}`,
5876
"component": Component,
77+
parameters: {
78+
docs: {
79+
description: {
80+
component: "Some component _markdown_"
81+
}
82+
}
83+
},
5984
"argTypes": {
6085
"containerWidth": {
6186
"control": {
6287
"type": "range",
6388
"min": 0,
6489
"max": 1920,
65-
"step": 1
90+
"step": 10
91+
}
92+
},
93+
"isFirstStory": {
94+
"table": {
95+
"disable": true
6696
}
6797
},
6898
...argTypes

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"format": "yarn _format --write",
2222
"format:check": "yarn _format --list-different",
2323
"grant_exec_perms": "node dist/bin/tools/grant_exec_perms",
24-
"storybook": "yarn build && ((tsc -w -p src) & start-storybook -p 6006)",
24+
"storybook": "yarn build && start-storybook -p 6006",
2525
"build-storybook": "yarn build && build-storybook"
2626
},
2727
"bin": {

src/lib/start.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ export async function startDsfrReact(params: Params) {
2323
].join(" ")
2424
);
2525

26-
assert(!isStarted, `${symToStr({ startDsfrReact })}() should be called only once`);
26+
if (isStarted) {
27+
return;
28+
}
2729

2830
isStarted = true;
2931

0 commit comments

Comments
 (0)