-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathregister.tsx
More file actions
69 lines (60 loc) · 1.92 KB
/
register.tsx
File metadata and controls
69 lines (60 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import React from "react";
import { addons, types, useParameter } from "storybook/manager-api";
import { AddonPanel } from "storybook/internal/components";
import { AddonName, PanelName, ParameterName } from "../constants";
import { ErrorBoundary } from "./components/ErrorBoundary";
import type { Config } from "../config";
import { Wrapper } from "./containers/Wrapper";
const DEFAULT_TAB_NAME = "Design";
export function register(renderTarget: "panel" | "tab") {
addons.register(AddonName, (api) => {
const title = function () {
const param = useParameter<Config | Config[] | undefined>(ParameterName);
if (!param) {
return DEFAULT_TAB_NAME;
}
// As the addon shows an additional tab panel, it's better not to
// use any of items' name.
if (Array.isArray(param)) {
return param.length > 0
? `${DEFAULT_TAB_NAME} (${param.length})`
: DEFAULT_TAB_NAME;
}
return (param.name || DEFAULT_TAB_NAME) + " (1)";
};
if (renderTarget === "tab") {
addons.add(PanelName, {
title: DEFAULT_TAB_NAME,
render({ active }) {
if (!active) {
// NOTE: Return type of render is `ReactElement`, hence returning `null` causes
// type error. I'm using `<noscript>` in place of `null`.
return <noscript />;
}
return (
<ErrorBoundary>
<Wrapper active />
</ErrorBoundary>
);
},
type: types.TAB,
paramKey: ParameterName,
});
} else {
addons.add(PanelName, {
type: types.PANEL,
title,
paramKey: ParameterName,
render({ active }) {
return (
<AddonPanel active={!!active}>
<ErrorBoundary>
<Wrapper active={!!active} />
</ErrorBoundary>
</AddonPanel>
);
},
});
}
});
}