mirror of
https://github.com/bitwarden/clients.git
synced 2026-11-16 07:03:29 +00:00
[CL-1285] create Storybook feature-flags addon in new @bitwarden/storybook lib (#21913)
* Add @bitwarden/storybook lib with feature-flags addon
Introduce a dedicated @bitwarden/storybook library housing Storybook
addons and utilities.
feature-flags addon:
- Manager-side "Feature Flags" panel listing every boolean flag from the
FeatureFlag enum; toggles write to a Storybook global.
- Preview-side mock ConfigService whose getFeatureFlag$ values are driven
by that global (reactive, so toggles update stories live).
- Flag catalog seeded into globals so the manager bundle never imports
@bitwarden/* (esbuild can't resolve those aliases).
- featureFlagModes() helper for opt-in Chromatic on/off snapshot modes.
- Documentation/Feature Flags MDX page describing panel + modes usage.
Also:
- Move .storybook/format-args-for-code-snippet into the lib and repoint
the 19 component stories to import from @bitwarden/storybook.
- Enable jsx ("react") in tsconfig.base.json for the manager panel (.tsx);
the Storybook manager builder emits the classic runtime.
- .storybook wiring: preview registers the decorator + seeds globals,
manager loads the panel by relative path.
package-lock.json also picks up npm's normalization of a few pre-existing
entries on main (readdirp dedupe, encoding removal) alongside the new
workspace registration.
* Add enabledFlags helper for per-story flag state
- enabledFlags(...flags) builds a story-level `globals` override so one
story can render with a flag on and another with it off.
- featureFlagModes now reuses enabledFlags for a single source of truth.
- Document the per-story pattern in the Feature Flags MDX.
* Fix CI failures for the feature-flags addon
- Decorator: skip providing the mock ConfigService when a story already
provides one at the application root (applicationConfig). Fixes the
danger-zone story crash "Cannot mix multi providers and regular
providers" — it registers ConfigService as `multi: true`.
- Lib jest: set isolatedModules in tsconfig.spec.json so ts-jest
transpiles without type-checking; its node10 resolution can't read
@storybook/angular's exports-based types (typecheck is covered by tsc).
- Reformat 5 component stories that were left prettier-dirty after the
import repoint (pre-commit runs prettier before eslint's import sort).
* Remove comments from tsconfig.spec.json and manager.js
This commit is contained in:
1
.github/CODEOWNERS
vendored
1
.github/CODEOWNERS
vendored
@@ -207,6 +207,7 @@ apps/desktop/src/services/encrypted-message-handler.service.ts @bitwarden/team
|
||||
.github/workflows/chromatic.yml @bitwarden/team-ui-foundation
|
||||
.storybook @bitwarden/team-ui-foundation
|
||||
libs/components @bitwarden/team-ui-foundation
|
||||
libs/storybook @bitwarden/team-ui-foundation
|
||||
libs/assets @bitwarden/team-ui-foundation
|
||||
libs/ui @bitwarden/team-ui-foundation
|
||||
libs/angular/src/scss @bitwarden/team-ui-foundation
|
||||
|
||||
@@ -10,6 +10,8 @@ const require = createRequire(import.meta.url);
|
||||
|
||||
const config: StorybookConfig = {
|
||||
stories: [
|
||||
"../libs/storybook/src/**/*.mdx",
|
||||
"../libs/storybook/src/**/*.stories.@(js|jsx|ts|tsx)",
|
||||
"../libs/auth/src/**/*.mdx",
|
||||
"../libs/auth/src/**/*.stories.@(js|jsx|ts|tsx)",
|
||||
"../libs/dirt/card/src/**/*.mdx",
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import { addons } from "storybook/manager-api";
|
||||
import { create } from "storybook/theming";
|
||||
|
||||
import "../libs/storybook/src/addons/feature-flags/panel";
|
||||
|
||||
const lightTheme = create({
|
||||
base: "light",
|
||||
//logo and Title
|
||||
|
||||
@@ -4,6 +4,13 @@ import { withThemeByClassName } from "@storybook/addon-themes";
|
||||
import { applicationConfig, componentWrapperDecorator } from "@storybook/angular";
|
||||
import type { Preview } from "@storybook/angular";
|
||||
|
||||
import {
|
||||
featureFlagDecorator,
|
||||
FEATURE_FLAG_CATALOG,
|
||||
FEATURE_FLAGS_CATALOG_GLOBAL,
|
||||
FEATURE_FLAGS_GLOBAL,
|
||||
} from "@bitwarden/storybook";
|
||||
|
||||
import docJson from "../documentation.json";
|
||||
|
||||
setCompodocJson(docJson);
|
||||
@@ -21,6 +28,7 @@ const preview: Preview = {
|
||||
applicationConfig({
|
||||
providers: [provideZoneChangeDetection()],
|
||||
}),
|
||||
featureFlagDecorator,
|
||||
withThemeByClassName({
|
||||
themes: {
|
||||
light: "theme_light",
|
||||
@@ -59,6 +67,13 @@ const preview: Preview = {
|
||||
disabled: true,
|
||||
},
|
||||
},
|
||||
initialGlobals: {
|
||||
[FEATURE_FLAGS_GLOBAL]: [],
|
||||
// Seeded here (preview can read the enum) so the manager panel can read the
|
||||
// catalog from globals without importing `@bitwarden/*`. Unchanged globals
|
||||
// stay out of the URL, so this large value never bloats story links.
|
||||
[FEATURE_FLAGS_CATALOG_GLOBAL]: FEATURE_FLAG_CATALOG,
|
||||
},
|
||||
tags: ["autodocs"],
|
||||
};
|
||||
|
||||
|
||||
@@ -67,6 +67,7 @@ module.exports = {
|
||||
"<rootDir>/libs/scheduling/jest.config.js",
|
||||
"<rootDir>/libs/organization-invite-link/jest.config.js",
|
||||
"<rootDir>/libs/pam/jest.config.js",
|
||||
"<rootDir>/libs/storybook/jest.config.js",
|
||||
],
|
||||
|
||||
// Workaround for a memory leak that crashes tests in CI:
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Meta, moduleMetadata, StoryObj } from "@storybook/angular";
|
||||
|
||||
import { formatArgsForCodeSnippet } from "../../../../.storybook/format-args-for-code-snippet";
|
||||
import { formatArgsForCodeSnippet } from "@bitwarden/storybook";
|
||||
|
||||
import { BadgeComponent } from "../badge";
|
||||
import { IconTileComponent } from "../icon-tile";
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Meta, StoryObj } from "@storybook/angular";
|
||||
|
||||
import { formatArgsForCodeSnippet } from "../../../../.storybook/format-args-for-code-snippet";
|
||||
import { formatArgsForCodeSnippet } from "@bitwarden/storybook";
|
||||
|
||||
import { AvatarComponent } from "./avatar.component";
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { Meta, StoryObj, moduleMetadata } from "@storybook/angular";
|
||||
|
||||
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
|
||||
import { formatArgsForCodeSnippet } from "@bitwarden/storybook";
|
||||
|
||||
import { formatArgsForCodeSnippet } from "../../../../.storybook/format-args-for-code-snippet";
|
||||
import { I18nMockService } from "../utils/i18n-mock.service";
|
||||
|
||||
import { BadgeListComponent } from "./badge-list.component";
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { CommonModule } from "@angular/common";
|
||||
import { Meta, moduleMetadata, StoryObj } from "@storybook/angular";
|
||||
|
||||
import { formatArgsForCodeSnippet } from "../../../../.storybook/format-args-for-code-snippet";
|
||||
import { formatArgsForCodeSnippet } from "@bitwarden/storybook";
|
||||
|
||||
import { BadgeComponent } from "./badge.component";
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { Meta, moduleMetadata, StoryObj } from "@storybook/angular";
|
||||
|
||||
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
|
||||
import { formatArgsForCodeSnippet } from "@bitwarden/storybook";
|
||||
|
||||
import { formatArgsForCodeSnippet } from "../../../../.storybook/format-args-for-code-snippet";
|
||||
import { ButtonModule } from "../button";
|
||||
import { IconButtonModule } from "../icon-button";
|
||||
import { LinkModule } from "../link";
|
||||
|
||||
@@ -3,6 +3,7 @@ import { Router, RouterModule } from "@angular/router";
|
||||
import { Meta, StoryObj, applicationConfig, moduleMetadata } from "@storybook/angular";
|
||||
|
||||
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
|
||||
import { formatArgsForCodeSnippet } from "@bitwarden/storybook";
|
||||
|
||||
import { IconButtonModule } from "../icon-button";
|
||||
import { LinkModule } from "../link";
|
||||
@@ -12,7 +13,6 @@ import { I18nMockService } from "../utils";
|
||||
import { BreadcrumbComponent } from "./breadcrumb.component";
|
||||
import { BreadcrumbsComponent } from "./breadcrumbs.component";
|
||||
|
||||
import { formatArgsForCodeSnippet } from ".storybook/format-args-for-code-snippet";
|
||||
@Component({
|
||||
template: /*html*/ ` <div class="tw-mt-5">Some really cool content for {{ currentUrl }}</div> `,
|
||||
changeDetection: ChangeDetectionStrategy.OnPush,
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { Meta, moduleMetadata, StoryObj } from "@storybook/angular";
|
||||
|
||||
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
|
||||
import { formatArgsForCodeSnippet } from "@bitwarden/storybook";
|
||||
|
||||
import { formatArgsForCodeSnippet } from "../../../../.storybook/format-args-for-code-snippet";
|
||||
import { ButtonType, ButtonTypes } from "../shared/button-like.abstraction";
|
||||
import { TypographyModule } from "../typography";
|
||||
import { I18nMockService } from "../utils/i18n-mock.service";
|
||||
|
||||
@@ -2,8 +2,8 @@ import { applicationConfig, Meta, StoryObj } from "@storybook/angular";
|
||||
|
||||
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
|
||||
import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service";
|
||||
import { formatArgsForCodeSnippet } from "@bitwarden/storybook";
|
||||
|
||||
import { formatArgsForCodeSnippet } from "../../../../.storybook/format-args-for-code-snippet";
|
||||
import { I18nMockService } from "../utils";
|
||||
|
||||
import { ColorPasswordComponent } from "./color-password.component";
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { Meta, moduleMetadata, StoryObj } from "@storybook/angular";
|
||||
|
||||
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
|
||||
import { formatArgsForCodeSnippet } from "@bitwarden/storybook";
|
||||
|
||||
import { formatArgsForCodeSnippet } from "../../../../.storybook/format-args-for-code-snippet";
|
||||
import { ButtonType, ButtonTypes } from "../shared/button-like.abstraction";
|
||||
import { TypographyModule } from "../typography";
|
||||
import { I18nMockService } from "../utils";
|
||||
|
||||
@@ -4,6 +4,7 @@ import { userEvent } from "storybook/test";
|
||||
|
||||
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
|
||||
import { GlobalStateProvider } from "@bitwarden/state";
|
||||
import { formatArgsForCodeSnippet } from "@bitwarden/storybook";
|
||||
|
||||
import { CalloutModule } from "../callout";
|
||||
import { NavigationModule } from "../navigation";
|
||||
@@ -14,8 +15,6 @@ import { StorybookGlobalStateProvider } from "../utils/state-mock";
|
||||
import { LayoutComponent } from "./layout.component";
|
||||
import { mockLayoutI18n } from "./mocks";
|
||||
|
||||
import { formatArgsForCodeSnippet } from ".storybook/format-args-for-code-snippet";
|
||||
|
||||
export default {
|
||||
title: "Component Library/Layout",
|
||||
component: LayoutComponent,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Meta, StoryObj, moduleMetadata } from "@storybook/angular";
|
||||
|
||||
import { formatArgsForCodeSnippet } from "../../../../.storybook/format-args-for-code-snippet";
|
||||
import { formatArgsForCodeSnippet } from "@bitwarden/storybook";
|
||||
|
||||
import { LinkComponent, LinkTypes } from "./link.component";
|
||||
import { LinkModule } from "./link.module";
|
||||
|
||||
@@ -2,8 +2,8 @@ import { Meta, StoryObj, moduleMetadata } from "@storybook/angular";
|
||||
|
||||
import { SpotReport } from "@bitwarden/assets/svg";
|
||||
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
|
||||
import { formatArgsForCodeSnippet } from "@bitwarden/storybook";
|
||||
|
||||
import { formatArgsForCodeSnippet } from "../../../../.storybook/format-args-for-code-snippet";
|
||||
import { FormFieldModule } from "../form-field";
|
||||
import { SvgComponent } from "../svg/svg.component";
|
||||
import { I18nMockService } from "../utils/i18n-mock.service";
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { Meta, StoryObj, moduleMetadata } from "@storybook/angular";
|
||||
|
||||
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
|
||||
import { formatArgsForCodeSnippet } from "@bitwarden/storybook";
|
||||
|
||||
import { formatArgsForCodeSnippet } from "../../../../.storybook/format-args-for-code-snippet";
|
||||
import { FormFieldModule } from "../form-field";
|
||||
import { I18nMockService } from "../utils/i18n-mock.service";
|
||||
|
||||
|
||||
@@ -2,8 +2,8 @@ import { FormsModule, ReactiveFormsModule } from "@angular/forms";
|
||||
import { Meta, StoryObj, moduleMetadata } from "@storybook/angular";
|
||||
|
||||
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
|
||||
import { formatArgsForCodeSnippet } from "@bitwarden/storybook";
|
||||
|
||||
import { formatArgsForCodeSnippet } from "../../../../.storybook/format-args-for-code-snippet";
|
||||
import { InputModule } from "../input/input.module";
|
||||
import { I18nMockService } from "../utils/i18n-mock.service";
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import { Meta, StoryObj } from "@storybook/angular";
|
||||
|
||||
import { SkeletonTextComponent } from "./skeleton-text.component";
|
||||
import { formatArgsForCodeSnippet } from "@bitwarden/storybook";
|
||||
|
||||
import { formatArgsForCodeSnippet } from ".storybook/format-args-for-code-snippet";
|
||||
import { SkeletonTextComponent } from "./skeleton-text.component";
|
||||
|
||||
export default {
|
||||
title: "Component Library/Skeleton/Skeleton Text",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Meta, StoryObj } from "@storybook/angular";
|
||||
|
||||
import { formatArgsForCodeSnippet } from "../../../../.storybook/format-args-for-code-snippet";
|
||||
import { formatArgsForCodeSnippet } from "@bitwarden/storybook";
|
||||
|
||||
import { SkeletonComponent } from "./skeleton.component";
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ import { FormsModule, ReactiveFormsModule, FormControl, FormGroup } from "@angul
|
||||
import { Meta, moduleMetadata, StoryObj, componentWrapperDecorator } from "@storybook/angular";
|
||||
|
||||
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
|
||||
import { formatArgsForCodeSnippet } from "@bitwarden/storybook";
|
||||
|
||||
import { FormControlModule } from "../form-control";
|
||||
import { FormControlCardComponent } from "../form-control/form-control-card.component";
|
||||
@@ -10,8 +11,6 @@ import { I18nMockService } from "../utils/i18n-mock.service";
|
||||
|
||||
import { SwitchComponent } from "./switch.component";
|
||||
|
||||
import { formatArgsForCodeSnippet } from ".storybook/format-args-for-code-snippet";
|
||||
|
||||
export default {
|
||||
title: "Component Library/Form/Switch",
|
||||
component: SwitchComponent,
|
||||
|
||||
@@ -5,8 +5,8 @@ import { Meta, StoryObj, applicationConfig, moduleMetadata } from "@storybook/an
|
||||
import { action } from "storybook/actions";
|
||||
|
||||
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
|
||||
import { formatArgsForCodeSnippet } from "@bitwarden/storybook";
|
||||
|
||||
import { formatArgsForCodeSnippet } from "../../../../.storybook/format-args-for-code-snippet";
|
||||
import { ButtonModule } from "../button";
|
||||
import { I18nMockService } from "../utils/i18n-mock.service";
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ import { Meta, StoryObj, moduleMetadata } from "@storybook/angular";
|
||||
import { getByRole, userEvent } from "storybook/test";
|
||||
|
||||
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
|
||||
import { formatArgsForCodeSnippet } from "@bitwarden/storybook";
|
||||
|
||||
import { ButtonComponent } from "../button";
|
||||
import { BitIconButtonComponent } from "../icon-button";
|
||||
@@ -12,8 +13,6 @@ import { TooltipPosition, TooltipPositionIdentifier, tooltipPositions } from "./
|
||||
import { TOOLTIP_DATA, TooltipComponent } from "./tooltip.component";
|
||||
import { TooltipDirective } from "./tooltip.directive";
|
||||
|
||||
import { formatArgsForCodeSnippet } from ".storybook/format-args-for-code-snippet";
|
||||
|
||||
export default {
|
||||
title: "Component Library/Tooltip",
|
||||
component: TooltipDirective,
|
||||
|
||||
5
libs/storybook/README.md
Normal file
5
libs/storybook/README.md
Normal file
@@ -0,0 +1,5 @@
|
||||
# storybook
|
||||
|
||||
Owned by: ui-foundation
|
||||
|
||||
Storybook addons and utilities for Bitwarden
|
||||
3
libs/storybook/eslint.config.mjs
Normal file
3
libs/storybook/eslint.config.mjs
Normal file
@@ -0,0 +1,3 @@
|
||||
import baseConfig from "../../eslint.config.mjs";
|
||||
|
||||
export default [...baseConfig];
|
||||
10
libs/storybook/jest.config.js
Normal file
10
libs/storybook/jest.config.js
Normal file
@@ -0,0 +1,10 @@
|
||||
module.exports = {
|
||||
displayName: "storybook",
|
||||
preset: "../../jest.preset.js",
|
||||
testEnvironment: "node",
|
||||
transform: {
|
||||
"^.+\\.[tj]s$": ["ts-jest", { tsconfig: "<rootDir>/tsconfig.spec.json" }],
|
||||
},
|
||||
moduleFileExtensions: ["ts", "js", "html"],
|
||||
coverageDirectory: "../../coverage/libs/storybook",
|
||||
};
|
||||
11
libs/storybook/package.json
Normal file
11
libs/storybook/package.json
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"name": "@bitwarden/storybook",
|
||||
"version": "0.0.1",
|
||||
"description": "Storybook addons and utilities for Bitwarden",
|
||||
"private": true,
|
||||
"type": "commonjs",
|
||||
"main": "index.js",
|
||||
"types": "index.d.ts",
|
||||
"license": "GPL-3.0",
|
||||
"author": "ui-foundation"
|
||||
}
|
||||
34
libs/storybook/project.json
Normal file
34
libs/storybook/project.json
Normal file
@@ -0,0 +1,34 @@
|
||||
{
|
||||
"name": "storybook",
|
||||
"$schema": "../../node_modules/nx/schemas/project-schema.json",
|
||||
"sourceRoot": "libs/storybook/src",
|
||||
"projectType": "library",
|
||||
"tags": [],
|
||||
"targets": {
|
||||
"build": {
|
||||
"executor": "@nx/js:tsc",
|
||||
"outputs": ["{options.outputPath}"],
|
||||
"options": {
|
||||
"outputPath": "dist/libs/storybook",
|
||||
"main": "libs/storybook/src/index.ts",
|
||||
"tsConfig": "libs/storybook/tsconfig.lib.json",
|
||||
"assets": ["libs/storybook/*.md"],
|
||||
"rootDir": "libs/storybook/src"
|
||||
}
|
||||
},
|
||||
"lint": {
|
||||
"executor": "@nx/eslint:lint",
|
||||
"outputs": ["{options.outputFile}"],
|
||||
"options": {
|
||||
"lintFilePatterns": ["libs/storybook/**/*.ts"]
|
||||
}
|
||||
},
|
||||
"test": {
|
||||
"executor": "@nx/jest:jest",
|
||||
"outputs": ["{workspaceRoot}/coverage/{projectRoot}"],
|
||||
"options": {
|
||||
"jestConfig": "libs/storybook/jest.config.js"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
98
libs/storybook/src/addons/feature-flags/config.ts
Normal file
98
libs/storybook/src/addons/feature-flags/config.ts
Normal file
@@ -0,0 +1,98 @@
|
||||
/**
|
||||
* Preview-side (iframe) wiring for the feature-flags addon.
|
||||
*
|
||||
* Runs in the preview bundle, which resolves `@bitwarden/*` aliases, so it can
|
||||
* read the flag enum directly. It provides a mock `ConfigService` whose flag
|
||||
* values are driven by the `FEATURE_FLAGS_GLOBAL` Storybook global.
|
||||
*/
|
||||
import { Decorator } from "@storybook/angular";
|
||||
import { BehaviorSubject, distinctUntilChanged, map, Observable } from "rxjs";
|
||||
|
||||
import {
|
||||
DefaultFeatureFlagValue,
|
||||
FeatureFlag,
|
||||
FeatureFlagValueType,
|
||||
} from "@bitwarden/common/enums/feature-flag.enum";
|
||||
import { ConfigService } from "@bitwarden/common/platform/abstractions/config/config.service";
|
||||
|
||||
import { FEATURE_FLAGS_GLOBAL, FeatureFlagOption } from "./constants";
|
||||
|
||||
/**
|
||||
* The set of enabled flags, driven by the panel via the decorator. Held in a
|
||||
* subject (rather than baked into the mock at construction) so `getFeatureFlag$`
|
||||
* consumers update live — Storybook re-renders on a globals change but does not
|
||||
* re-bootstrap the Angular app, so a per-render provider would be ignored.
|
||||
*/
|
||||
const enabledFlags$ = new BehaviorSubject<ReadonlySet<string>>(new Set());
|
||||
|
||||
/**
|
||||
* A mock `ConfigService` reporting the enabled flags as `true` and every other
|
||||
* flag at its default value.
|
||||
*/
|
||||
class StorybookConfigService implements Partial<ConfigService> {
|
||||
getFeatureFlag$ = <Flag extends FeatureFlag>(key: Flag): Observable<FeatureFlagValueType<Flag>> =>
|
||||
enabledFlags$.pipe(
|
||||
map((enabled) => this.valueFor(key, enabled)),
|
||||
distinctUntilChanged(),
|
||||
);
|
||||
|
||||
getFeatureFlag = <Flag extends FeatureFlag>(key: Flag): Promise<FeatureFlagValueType<Flag>> =>
|
||||
Promise.resolve(this.valueFor(key, enabledFlags$.value));
|
||||
|
||||
private valueFor<Flag extends FeatureFlag>(
|
||||
key: Flag,
|
||||
enabled: ReadonlySet<string>,
|
||||
): FeatureFlagValueType<Flag> {
|
||||
if (enabled.has(key)) {
|
||||
// The panel only toggles boolean flags, so "enabled" always means `true`.
|
||||
return true as FeatureFlagValueType<Flag>;
|
||||
}
|
||||
// Cast needed because indexing by a generic `Flag` widens to the union of
|
||||
// all flag value types rather than narrowing to `FeatureFlagValueType<Flag>`.
|
||||
return DefaultFeatureFlagValue[key] as FeatureFlagValueType<Flag>;
|
||||
}
|
||||
}
|
||||
|
||||
const configService = new StorybookConfigService();
|
||||
|
||||
/**
|
||||
* The catalog of toggleable flags, seeded into globals for the manager panel.
|
||||
* Restricted to boolean flags — the only ones a checkbox can meaningfully drive.
|
||||
*/
|
||||
export const FEATURE_FLAG_CATALOG: FeatureFlagOption[] = Object.entries(FeatureFlag)
|
||||
.filter(([, value]) => typeof DefaultFeatureFlagValue[value as FeatureFlag] === "boolean")
|
||||
.map(([name, value]) => ({ name, value: value as string }))
|
||||
.sort((a, b) => a.name.localeCompare(b.name));
|
||||
|
||||
/**
|
||||
* Provides the flag-driven mock `ConfigService` at the application root of every
|
||||
* story and syncs the enabled set from globals on each render.
|
||||
*
|
||||
* A story that provides its own `ConfigService` is left untouched: one provided
|
||||
* via `moduleMetadata` sits in a child injector and shadows this one, while one
|
||||
* provided at the application root (`applicationConfig`) is detected here and we
|
||||
* skip adding ours — adding a second would clash (e.g. Angular refuses to mix a
|
||||
* `multi` provider with a regular one for the same token).
|
||||
*/
|
||||
export const featureFlagDecorator: Decorator = (storyFn, context) => {
|
||||
const enabled = (context.globals[FEATURE_FLAGS_GLOBAL] as string[] | undefined) ?? [];
|
||||
enabledFlags$.next(new Set(enabled));
|
||||
const story = storyFn();
|
||||
const providers = story.applicationConfig?.providers ?? [];
|
||||
const storyProvidesConfig = providers.some(
|
||||
(p) =>
|
||||
p != null &&
|
||||
typeof p === "object" &&
|
||||
"provide" in p &&
|
||||
(p as { provide: unknown }).provide === ConfigService,
|
||||
);
|
||||
return {
|
||||
...story,
|
||||
applicationConfig: {
|
||||
...story.applicationConfig,
|
||||
providers: storyProvidesConfig
|
||||
? providers
|
||||
: [{ provide: ConfigService, useValue: configService }, ...providers],
|
||||
},
|
||||
};
|
||||
};
|
||||
10
libs/storybook/src/addons/feature-flags/constants.spec.ts
Normal file
10
libs/storybook/src/addons/feature-flags/constants.spec.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import { FEATURE_FLAGS_GLOBAL, featureFlagModes } from "./constants";
|
||||
|
||||
describe("featureFlagModes", () => {
|
||||
it("builds off and on Chromatic modes for the given flags", () => {
|
||||
const modes = featureFlagModes("flag-a", "flag-b");
|
||||
|
||||
expect(modes["flag off"]).toEqual({ [FEATURE_FLAGS_GLOBAL]: [] });
|
||||
expect(modes["flag on"]).toEqual({ [FEATURE_FLAGS_GLOBAL]: ["flag-a", "flag-b"] });
|
||||
});
|
||||
});
|
||||
55
libs/storybook/src/addons/feature-flags/constants.ts
Normal file
55
libs/storybook/src/addons/feature-flags/constants.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
/**
|
||||
* Shared, dependency-free constants for the feature-flags Storybook addon.
|
||||
*
|
||||
* This file must not import from `@bitwarden/*` so it can be safely bundled into
|
||||
* BOTH the preview (iframe) and the manager (Storybook UI shell), which use
|
||||
* different builders. The manager bundle in particular cannot resolve the
|
||||
* `@bitwarden/*` path aliases.
|
||||
*/
|
||||
|
||||
export const ADDON_ID = "bitwarden/feature-flags";
|
||||
export const PANEL_ID = `${ADDON_ID}/panel`;
|
||||
|
||||
/** Global holding the list of currently-enabled feature flag keys. */
|
||||
export const FEATURE_FLAGS_GLOBAL = "bwEnabledFeatureFlags";
|
||||
|
||||
/**
|
||||
* Global holding the full catalog of toggleable flags. Seeded by the preview
|
||||
* (which can read the enum) via `initialGlobals` and read by the manager panel.
|
||||
* Globals sync across the preview/manager boundary, so the panel never needs to
|
||||
* import the flag enum itself.
|
||||
*/
|
||||
export const FEATURE_FLAGS_CATALOG_GLOBAL = "bwFeatureFlagCatalog";
|
||||
|
||||
export type FeatureFlagOption = { name: string; value: string };
|
||||
|
||||
/**
|
||||
* Builds a story-level `globals` override that enables the given flag(s) (and
|
||||
* only those) for a single story. Use it to define distinct stories with
|
||||
* different flag states — the override takes precedence over the panel:
|
||||
*
|
||||
* ```ts
|
||||
* export const FlagOn = { globals: enabledFlags(FeatureFlag.Foobar) };
|
||||
* export const FlagOff = { globals: enabledFlags() };
|
||||
* ```
|
||||
*/
|
||||
export function enabledFlags(...flags: string[]) {
|
||||
return { [FEATURE_FLAGS_GLOBAL]: flags };
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a Chromatic `modes` map that snapshots a single story with the given
|
||||
* flag(s) off and then on. Opt in per-story:
|
||||
*
|
||||
* ```ts
|
||||
* export const Default = {
|
||||
* parameters: { chromatic: { modes: featureFlagModes(FeatureFlag.Foobar) } },
|
||||
* };
|
||||
* ```
|
||||
*/
|
||||
export function featureFlagModes(...flags: string[]) {
|
||||
return {
|
||||
"flag off": enabledFlags(),
|
||||
"flag on": enabledFlags(...flags),
|
||||
};
|
||||
}
|
||||
56
libs/storybook/src/addons/feature-flags/feature-flags.mdx
Normal file
56
libs/storybook/src/addons/feature-flags/feature-flags.mdx
Normal file
@@ -0,0 +1,56 @@
|
||||
import { Meta } from "@storybook/addon-docs/blocks";
|
||||
|
||||
<Meta title="Documentation/Feature Flags" />
|
||||
|
||||
# Feature Flags
|
||||
|
||||
The `@bitwarden/storybook` feature-flags addon lets you preview any story under different
|
||||
feature-flag states — interactively while developing, and as automated Chromatic snapshots. Both are
|
||||
driven by a mock `ConfigService` provided to every story.
|
||||
|
||||
## Toggle flags interactively
|
||||
|
||||
Open the **Feature Flags** panel (bottom tab, next to Controls). It lists every boolean flag from
|
||||
`FeatureFlag`. Ticking a flag reports it as `true` to the mock `ConfigService`; unticking returns it
|
||||
to its default value. Stories that read a flag via `getFeatureFlag$(...) | async` update live — no
|
||||
reload needed.
|
||||
|
||||
## A flag on in one story, off in another
|
||||
|
||||
To fix a flag's state per story — one story with it on, another with it off — set the story's
|
||||
`globals` with `enabledFlags`. The override takes precedence over whatever's toggled in the panel,
|
||||
so each story always renders in its own state:
|
||||
|
||||
```ts
|
||||
import { FeatureFlag } from "@bitwarden/common/enums/feature-flag.enum";
|
||||
import { enabledFlags } from "@bitwarden/storybook";
|
||||
|
||||
export const FlagOn: StoryObj<MyComponent> = {
|
||||
globals: enabledFlags(FeatureFlag.MyFlag),
|
||||
};
|
||||
|
||||
export const FlagOff: StoryObj<MyComponent> = {
|
||||
globals: enabledFlags(),
|
||||
};
|
||||
```
|
||||
|
||||
Pass multiple flags to enable them together: `enabledFlags(FlagA, FlagB)`.
|
||||
|
||||
## Snapshot a story with a flag off and on
|
||||
|
||||
Opt a story into Chromatic on/off snapshots with `featureFlagModes`:
|
||||
|
||||
```ts
|
||||
import { FeatureFlag } from "@bitwarden/common/enums/feature-flag.enum";
|
||||
import { featureFlagModes } from "@bitwarden/storybook";
|
||||
|
||||
export const Default: StoryObj<MyComponent> = {
|
||||
parameters: {
|
||||
chromatic: { modes: featureFlagModes(FeatureFlag.MyFlag) },
|
||||
},
|
||||
};
|
||||
```
|
||||
|
||||
Chromatic then captures the story twice — once with the flag off, once on. Pass multiple flags to
|
||||
enable them together: `featureFlagModes(FlagA, FlagB)`. Stories that don't opt in keep their single
|
||||
flag-off snapshot.
|
||||
4
libs/storybook/src/addons/feature-flags/index.ts
Normal file
4
libs/storybook/src/addons/feature-flags/index.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export * from "./constants";
|
||||
export * from "./config";
|
||||
// `./panel` is a manager entry (registers the addon as a side effect) and is
|
||||
// imported directly by `.storybook/manager.js`, not re-exported here.
|
||||
107
libs/storybook/src/addons/feature-flags/panel.tsx
Normal file
107
libs/storybook/src/addons/feature-flags/panel.tsx
Normal file
@@ -0,0 +1,107 @@
|
||||
/**
|
||||
* Manager-side (Storybook UI) "Feature Flags" panel.
|
||||
*
|
||||
* Renders a checkbox per flag from the catalog seeded into globals by the
|
||||
* preview, and writes the enabled set back to the `FEATURE_FLAGS_GLOBAL` global.
|
||||
* The preview's mock `ConfigService` reads that global. This file must not
|
||||
* import from `@bitwarden/*` — the manager bundle can't resolve those aliases,
|
||||
* which is why the catalog arrives via globals rather than a direct import. It
|
||||
* is imported directly (relatively) by `.storybook/manager.js` as a manager
|
||||
* entry, so it is intentionally not re-exported from the addon barrel.
|
||||
*/
|
||||
import React, { useMemo, useState } from "react";
|
||||
import { addons, types, useGlobals } from "storybook/manager-api";
|
||||
|
||||
import {
|
||||
ADDON_ID,
|
||||
FEATURE_FLAGS_CATALOG_GLOBAL,
|
||||
FEATURE_FLAGS_GLOBAL,
|
||||
FeatureFlagOption,
|
||||
PANEL_ID,
|
||||
} from "./constants";
|
||||
|
||||
function FeatureFlagsPanel() {
|
||||
const [globals, updateGlobals] = useGlobals();
|
||||
const [filter, setFilter] = useState("");
|
||||
|
||||
const catalog = (globals[FEATURE_FLAGS_CATALOG_GLOBAL] ?? []) as FeatureFlagOption[];
|
||||
const enabled = (globals[FEATURE_FLAGS_GLOBAL] ?? []) as string[];
|
||||
const enabledSet = useMemo(() => new Set(enabled), [enabled]);
|
||||
|
||||
const visible = useMemo(() => {
|
||||
const needle = filter.toLowerCase();
|
||||
return catalog.filter(
|
||||
(f) => f.name.toLowerCase().includes(needle) || f.value.toLowerCase().includes(needle),
|
||||
);
|
||||
}, [catalog, filter]);
|
||||
|
||||
const setEnabled = (next: string[]) => updateGlobals({ [FEATURE_FLAGS_GLOBAL]: next });
|
||||
const toggle = (value: string) =>
|
||||
setEnabled(enabledSet.has(value) ? enabled.filter((f) => f !== value) : [...enabled, value]);
|
||||
|
||||
return (
|
||||
<div
|
||||
style={{
|
||||
display: "flex",
|
||||
flexDirection: "column",
|
||||
height: "100%",
|
||||
boxSizing: "border-box",
|
||||
padding: 16,
|
||||
fontFamily: "sans-serif",
|
||||
fontSize: 13,
|
||||
}}
|
||||
>
|
||||
<div style={{ display: "flex", gap: 8, marginBottom: 12 }}>
|
||||
<input
|
||||
placeholder="Filter flags…"
|
||||
value={filter}
|
||||
onChange={(e) => setFilter(e.target.value)}
|
||||
style={{ flex: 1, padding: "4px 8px" }}
|
||||
/>
|
||||
<button
|
||||
onClick={() => setEnabled([...new Set([...enabled, ...visible.map((f) => f.value)])])}
|
||||
>
|
||||
Enable shown
|
||||
</button>
|
||||
<button onClick={() => setEnabled([])}>Clear all</button>
|
||||
</div>
|
||||
<div style={{ marginBottom: 8, opacity: 0.7 }}>
|
||||
{enabledSet.size} of {catalog.length} enabled
|
||||
</div>
|
||||
<ul
|
||||
style={{
|
||||
listStyle: "none",
|
||||
margin: 0,
|
||||
padding: 0,
|
||||
flex: 1,
|
||||
minHeight: 0,
|
||||
overflowY: "auto",
|
||||
}}
|
||||
>
|
||||
{visible.map((f) => (
|
||||
<li key={f.value} style={{ padding: "2px 0" }}>
|
||||
<label style={{ display: "flex", gap: 8, cursor: "pointer", alignItems: "baseline" }}>
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={enabledSet.has(f.value)}
|
||||
onChange={() => toggle(f.value)}
|
||||
/>
|
||||
<span>
|
||||
<strong>{f.name}</strong> <span style={{ opacity: 0.6 }}>{f.value}</span>
|
||||
</span>
|
||||
</label>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
addons.register(ADDON_ID, () => {
|
||||
addons.add(PANEL_ID, {
|
||||
type: types.PANEL,
|
||||
title: "Feature Flags",
|
||||
match: ({ viewMode }) => viewMode === "story",
|
||||
render: ({ active }) => (active ? <FeatureFlagsPanel /> : null),
|
||||
});
|
||||
});
|
||||
2
libs/storybook/src/index.ts
Normal file
2
libs/storybook/src/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from "./addons/feature-flags";
|
||||
export * from "./utils/format-args-for-code-snippet";
|
||||
6
libs/storybook/tsconfig.eslint.json
Normal file
6
libs/storybook/tsconfig.eslint.json
Normal file
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"extends": "../../tsconfig.base.json",
|
||||
"files": [],
|
||||
"include": ["src/**/*.ts", "src/**/*.tsx", "src/**/*.js"],
|
||||
"exclude": ["**/build", "**/dist"]
|
||||
}
|
||||
13
libs/storybook/tsconfig.json
Normal file
13
libs/storybook/tsconfig.json
Normal file
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"extends": "../../tsconfig.base.json",
|
||||
"files": [],
|
||||
"include": [],
|
||||
"references": [
|
||||
{
|
||||
"path": "./tsconfig.lib.json"
|
||||
},
|
||||
{
|
||||
"path": "./tsconfig.spec.json"
|
||||
}
|
||||
]
|
||||
}
|
||||
10
libs/storybook/tsconfig.lib.json
Normal file
10
libs/storybook/tsconfig.lib.json
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"extends": "./tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "../../dist/out-tsc",
|
||||
"declaration": true,
|
||||
"types": ["node"]
|
||||
},
|
||||
"include": ["src/**/*.ts", "src/**/*.tsx"],
|
||||
"exclude": ["jest.config.js", "src/**/*.spec.ts"]
|
||||
}
|
||||
11
libs/storybook/tsconfig.spec.json
Normal file
11
libs/storybook/tsconfig.spec.json
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"extends": "./tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "../../dist/out-tsc",
|
||||
"module": "commonjs",
|
||||
"moduleResolution": "node10",
|
||||
"isolatedModules": true,
|
||||
"types": ["jest", "node"]
|
||||
},
|
||||
"include": ["jest.config.ts", "src/**/*.test.ts", "src/**/*.spec.ts", "src/**/*.d.ts"]
|
||||
}
|
||||
9
package-lock.json
generated
9
package-lock.json
generated
@@ -38190,6 +38190,15 @@
|
||||
"node_modules/zxcvbn": {
|
||||
"version": "4.4.2",
|
||||
"license": "MIT"
|
||||
},
|
||||
"libs/storybook": {
|
||||
"name": "@bitwarden/storybook",
|
||||
"version": "0.0.1",
|
||||
"license": "GPL-3.0"
|
||||
},
|
||||
"node_modules/@bitwarden/storybook": {
|
||||
"resolved": "libs/storybook",
|
||||
"link": true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
"module": "ES2020",
|
||||
"lib": ["es5", "es6", "es7", "dom", "ES2021", "ESNext.Disposable"],
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"jsx": "react",
|
||||
"experimentalDecorators": true,
|
||||
"emitDecoratorMetadata": true,
|
||||
"declaration": false,
|
||||
@@ -73,7 +74,8 @@
|
||||
"@bitwarden/vault-export-core": ["./libs/tools/export-vault-core/src"],
|
||||
"@bitwarden/vault-export-ui": ["./libs/tools/export-vault-ui/src"],
|
||||
"@bitwarden/web-vault/*": ["./apps/web/src/*"],
|
||||
"@bitwarden/pam": ["./libs/pam/src/index.ts"]
|
||||
"@bitwarden/pam": ["./libs/pam/src/index.ts"],
|
||||
"@bitwarden/storybook": ["./libs/storybook/src/index.ts"]
|
||||
},
|
||||
"plugins": [
|
||||
{
|
||||
|
||||
@@ -4,8 +4,6 @@
|
||||
".storybook/main.ts",
|
||||
".storybook/manager.js",
|
||||
".storybook/test-runner.ts",
|
||||
".storybook/format-args-for-code-snippet.ts",
|
||||
".storybook/format-args-for-code-snippet.spec.ts",
|
||||
"apps/browser/src/autofill/content/components/.lit-storybook/main.ts"
|
||||
],
|
||||
"include": ["apps/**/*", "libs/**/*", "bitwarden_license/**/*", "scripts/**/*"],
|
||||
|
||||
Reference in New Issue
Block a user