-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathFigma.tsx
More file actions
56 lines (45 loc) · 1.57 KB
/
Figma.tsx
File metadata and controls
56 lines (45 loc) · 1.57 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
/** @jsx jsx */
import { FC, useMemo } from "react";
import { jsx } from "storybook/theming";
import { IFrame } from "./IFrame";
import { FigmaConfig, IFrameConfigBase } from "../../config";
export const figmaURLPattern =
/https:\/\/[\w.-]+\.?figma.com\/([\w-]+)\/([0-9a-zA-Z]{22,128})(?:\/.*)?$/;
export const isFigmaURL = (url: string) => figmaURLPattern.test(url);
interface Props {
config: FigmaConfig;
}
export const Figma: FC<Props> = ({ config }) => {
const iframeConfig = useMemo<IFrameConfigBase>(() => {
const isValid = isFigmaURL(config.url);
if (!isValid) {
console.warn(
"[storybook-addon-designs] " +
"The URL you specified is not valid Figma URL.\n" +
"The addon fallbacks to normal iframe mode." +
"For more detail, please check <https://www.figma.com/developers/embed>.",
);
return config;
}
const url = createEmbedURL(
config.url,
config.embedHost || location.hostname,
);
return {
url,
allowFullscreen: config.allowFullscreen,
};
}, [config.url, config.allowFullscreen, config.embedHost]);
return <IFrame defer config={iframeConfig} />;
};
function createEmbedURL(encodedURL: string, embedHost: string) {
const url = new URL(encodedURL);
url.hostname = url.hostname.replace(/^www\./, "embed.");
url.searchParams.delete("embed_origin");
url.searchParams.set("embed-host", embedHost);
for (const [key, value] of url.searchParams) {
url.searchParams.delete(key);
url.searchParams.set(key.replace(/_/g, "-"), value);
}
return url.href;
}