Skip to content

Commit

Permalink
feat: decorator can be used to hide story on web (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
dannyhw authored Jan 15, 2024
1 parent 2c748f8 commit 1cea516
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 3 deletions.
9 changes: 9 additions & 0 deletions .storybook/local-preset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function previewAnnotations(entry = []) {
return [...entry, require.resolve("../dist/preview.mjs")];
}

module.exports = {
previewAnnotations,
experimental_serverChannel:
require("../dist/preset.js").experimental_serverChannel,
};
2 changes: 1 addition & 1 deletion .storybook/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const config: StorybookConfig = {
"@storybook/addon-links",
"@storybook/addon-essentials",
"@storybook/addon-interactions",
"../dist/preset.mjs",
"./local-preset.js",
],
framework: {
name: "@storybook/react-vite",
Expand Down
1 change: 1 addition & 0 deletions .storybook/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Preview } from "@storybook/react";

const preview: Preview = {
parameters: {
deviceOnly: true,
backgrounds: {
default: "light",
},
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"require": "./dist/preset.js",
"import": "./dist/preset.mjs"
},
"./preview": "./dist/preview.mjs",
"./package.json": "./package.json"
},
"main": "dist/index.js",
Expand All @@ -34,14 +35,15 @@
"dist/**/*",
"README.md",
"*.js",
"*.d.ts"
"*.d.ts",
"preset.mjs",
"preview.mjs"
],
"scripts": {
"clean": "rimraf ./dist",
"prebuild": "npm run clean",
"build": "tsup",
"build:watch": "npm run build -- --watch",
"test": "echo \"Error: no test specified\" && exit 1",
"start": "run-p build:watch 'storybook --quiet'",
"prerelease": "zx scripts/prepublish-checks.mjs",
"release": "npm run build && auto shipit",
Expand Down
3 changes: 3 additions & 0 deletions preview.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
experimental_serverChannel: require("./dist/preview.js"),
};
1 change: 1 addition & 0 deletions preview.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./dist/preview";
29 changes: 29 additions & 0 deletions src/preview.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { ProjectAnnotations, Renderer } from "@storybook/types";
import React from "react";

/**
* Note: if you want to use JSX in this file, rename it to `preview.tsx`
* and update the entry prop in tsup.config.ts to use "src/preview.tsx",
*/

const preview: ProjectAnnotations<Renderer> = {
decorators: [
function OnDeviceOnly(Story, { parameters }) {
if (parameters.deviceOnly) {
return (
<span
style={{
fontFamily: `-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol"`,
}}
>
Parameter 'deviceOnly' was found and so the story is not rendered
</span>
);
}

return Story();
},
],
};

export default preview;
13 changes: 13 additions & 0 deletions tsup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ import { globalPackages as globalManagerPackages } from "@storybook/manager/glob
import { globalPackages as globalPreviewPackages } from "@storybook/preview/globals";

const NODE_TARGET: Options["target"] = ["node16"];
const BROWSER_TARGET: Options["target"] = [
"chrome100",
"safari15",
"firefox91",
];

export default defineConfig(async (options) => {
const commonConfig: Options = {
Expand All @@ -25,5 +30,13 @@ export default defineConfig(async (options) => {
platform: "node",
external: [...globalManagerPackages, ...globalPreviewPackages],
},
{
...commonConfig,
entry: ["src/preview.tsx"],
format: ["esm"],
target: BROWSER_TARGET,
platform: "browser",
external: globalPreviewPackages,
},
];
});

0 comments on commit 1cea516

Please sign in to comment.