Skip to content

Commit

Permalink
Merge pull request #72 from tetarchus/next
Browse files Browse the repository at this point in the history
Merge changes for V2
  • Loading branch information
tetarchus authored Oct 12, 2024
2 parents 8bc3ade + 9c73f60 commit 8f79051
Show file tree
Hide file tree
Showing 68 changed files with 11,912 additions and 15,542 deletions.
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20.17.0
20.18.0
21 changes: 15 additions & 6 deletions .storybook/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,23 @@ import type { Preview } from '@storybook/react';
const preview: Preview = {
parameters: {
badgesConfig: {
MyCustomBadge: {
title: 'My Custom Badge',
styles: {
backgroundColor: '#00C7AC',
borderColor: 'red',
color: '#FFFFFF',
badgeMap: {
MyCustomBadge: {
title: 'My Custom Badge',
styles: {
backgroundColor: '#00C7AC',
borderColor: 'red',
color: '#FFFFFF',
},
location: ['sidebar', 'toolbar', 'toolbar-end'],
},
Token: {
title: 'PH-CO-CMS-BTN-001',
location: ['sidebar', 'toolbar', 'toolbar-end'],
},
},
baseStyle: 'github',
useTags: true,
},
controls: {
matchers: {
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
[![npm version](https://badge.fury.io/js/storybook-addon-badges.svg)](https://www.npmjs.com/package/storybook-addon-badges)

<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->

[![All Contributors](https://img.shields.io/badge/all_contributors-1-orange.svg?style=flat-square)](#contributors-)

<!-- ALL-CONTRIBUTORS-BADGE:END -->

> [!TIP]
Expand Down Expand Up @@ -120,3 +122,13 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d

This project follows the [all-contributors](https://github.com/all-contributors/all-contributors)
specification. Contributions of any kind welcome!

## Roadmap

These are some future enhancements that I'd like to include:

- Sidebar badges show even when not selected (Blocked by a potential bug in Storybook)
- Ability to search for stories based on badge name (Also blocked by the same above bug)

If there's a feature that you'd like to see, please raise a
[feature request](https://github.com/tetarchus/storybook-addon-badges/issues/new)
File renamed without changes.
109 changes: 73 additions & 36 deletions docs/docs/customisation/adding-styles.mdx
Original file line number Diff line number Diff line change
@@ -1,42 +1,38 @@
---
sidebar_position: 2
sidebar_position: 3
title: Adding Styles
---
import { Badge, BADGE } from '@site/src/components';

By default, most badges appear mostly grey, however you can use the `styles` configuration to enhance your badges and add a different visual style for each one.
By default any string added to a story's `badges` parameter will be displayed based on a default style.

:::warning
You can override the default style for any of the [pre-defined-badges](../getting-started/predefined-badges.mdx), however this will overwrite the whole definition. If you would like to only overwrite _some_ of the properties, you can import their original config as `defaultBadgesConfig` and use spreading to only overwrite certain properties.
To change this default style, you can either update the `badgesConfig.baseStyle` parameter, or define a [custom badge](./create-custom-badges.mdx) and define its specific style.

<details>
<summary>Example</summary>
## Defining a base style

```ts title=".storybook/preview.ts"
import { BADGE, defaultBadgesConfig } from 'storybook-addon-badges';
The `baseStyle` key of the addon config defines the style that all other badges will inherit by default. This can either be defined as a preset style, or as a custom style that extends one of the presets (this is to ensure that all required values are present).

import type { Preview } from '@storybook/[your-framework]';
The available base styles are `'default'` for the standard style, and `'github'` for a style similar to Github's labels:

const preview: Preview = {
//...other preview config
parameters: {
//...other parameters
badgesConfig: {
[BADGE.BETA]: {
...defaultBadgesConfig[BADGE.BETA],
styles: {
...defaultBadgesConfig[BADGE.BETA].styles,
backgroundColor: "#00C7AC",
}
}
}
}
}
- `default`: <Badge badge={BADGE.DEFAULT} />
- `github`: <Badge badge={BADGE.DEFAULT} baseStyle='github'/>

export default preview;
```
```ts
baseStyle: 'default',
```

</details>
:::
Alternatively you can provide your own `BadgeStyle` object, containing any of the [available style properties](#available-style-properties). If providing a style object, it must include a `base` key set to either `'default'` or `'github'`. Any values not provided will fall back to the values from those preset styles:

```ts
baseStyle: {
base: 'github',
borderWidth: "3px",
}
```

## Defining a custom badge style

To define a style for a specific badge, pass in a `BadgeStyle` object to its configuration in `badgesConfig.badgeMap`. This can be a partial style, and any values not provided will fall back to the `badgesConfig.baseStyle` values.

```ts title=".storybook/preview.ts"
import type { Preview } from '@storybook/[your-framework]';
Expand All @@ -46,21 +42,25 @@ const preview: Preview = {
parameters: {
//...other parameters
badgesConfig: {
MyCustomBadge: {
title: 'My Custom Badge',
styles: {
backgroundColor: "#00C7AC",
borderColor: "red",
color: "#FFFFFF"
badgeMap: {
MyCustomBadge: {
title: 'My Custom Badge',
styles: {
backgroundColor: '#00C7AC',
borderColor: 'red',
color: '#FFFFFF'
}
}
}
},
baseStyle: 'github'
}
}
}

export default preview;
```


## Available Style Properties

Theming is limited to a subset of CSS properties, if there are any you need simply raise a [feature request](https://github.com/tetarchus/storybook-addon-badges/issues).
Expand All @@ -79,4 +79,41 @@ Theming is limited to a subset of CSS properties, if there are any you need simp
| lineHeight | 1 |
| paddingBlock | 2px |
| paddingInline | 5px |
| textTransform | uppercase |
| textTransform | uppercase |

{/* TODO: Validate and remove */}
{/*
:::warning
You can override the default style for any of the [pre-defined-badges](../getting-started/predefined-badges.mdx), however this will overwrite the whole definition. If you would like to only overwrite _some_ of the properties, you can import their original config as `defaultBadgesConfig` and use spreading to only overwrite certain properties.
<details>
<summary>Example</summary>
```ts title=".storybook/preview.ts"
import { BADGE, defaultBadgesConfig } from 'storybook-addon-badges';
import type { Preview } from '@storybook/[your-framework]';
const preview: Preview = {
//...other preview config
parameters: {
//...other parameters
badgesConfig: {
badgeMap: {
[BADGE.BETA]: {
...defaultBadgesConfig[BADGE.BETA],
styles: {
...defaultBadgesConfig[BADGE.BETA].styles,
backgroundColor: "#00C7AC",
}
}
}
}
}
}
export default preview;
```
</details>
::: */}
30 changes: 17 additions & 13 deletions docs/docs/customisation/adding-tooltips.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
sidebar_position: 3
sidebar_position: 5
title: Adding Tooltips
---

Expand All @@ -17,9 +17,11 @@ const preview: Preview = {
parameters: {
//...other parameters
badgesConfig: {
MyCustomBadge: {
title: 'My Custom Badge',
tooltip: "This is a custom tooltip."
badgeMap: {
MyCustomBadge: {
title: 'My Custom Badge',
tooltip: "This is a custom tooltip."
}
}
}
}
Expand Down Expand Up @@ -50,15 +52,17 @@ const preview: Preview = {
parameters: {
//...other parameters
badgesConfig: {
MyCustomBadge: {
title: 'My Custom Badge',
tooltip: {
title: "This tooltip has a title and links!",
desc: "You can customise the content of the tooltip, and even pass in a custom React component",
links: [
{ title: 'Read more', href: 'http://path/to/your/docs' },
{ title: 'Leave feedback', onClick: () => alert('thanks for the feedback') },
]
badgeMap: {
MyCustomBadge: {
title: 'My Custom Badge',
tooltip: {
title: "This tooltip has a title and links!",
desc: "You can customise the content of the tooltip, and even pass in a custom React component",
links: [
{ title: 'Read more', href: 'http://path/to/your/docs' },
{ title: 'Leave feedback', onClick: () => alert('thanks for the feedback') },
]
}
}
}
}
Expand Down
65 changes: 65 additions & 0 deletions docs/docs/customisation/badge-locations.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
---
sidebar_position: 4
title: Badge Locations
---

Badges can appear in multiple locations within the Storybook UI. Current available locations are:

- `toolbar` - Display badges in the standard toolbar next to other controls.
- `toolbar-end` - Display badges at the end of the toolbar, to the right of the full-screen control.
- `sidebar` - Display badges in the sidebar next to the stories they apply to.

A badge definition can define multiple locations for a badge, and the default locations for all badges to inherit can also be defined in the addon configuration.

## Default locations configuration

To set the locations that apply to all badges, define them in the addon configuration parameter (`badgesConfig`) under `locations`. If a badge definition doesn't define its own `locations`, these will be used instead.
By default, all badges will show in the `toolbar` area only.

```ts title=".storybook/preview.ts"
import type { Preview } from '@storybook/[your-framework]';
import type { NewBadgesConfig } from 'storybook-addon-badges';

const preview: Preview = {
//...other preview config
parameters: {
//...other parameters
badgesConfig: {
locations: ['toolbar'],
// ...other addon config parameters
} satisfies NewBadgesConfig
}
}

export default preview;
```

If a specific badge needs to override the defaults, you can define `locations` in its configuration object. Note that for badges with no explicit config object, they will use the `default` value.

## Badge-specific locations configuration

To set a specific badge's location, define it under the `locations` key of its config.

```ts title=".storybook/preview.ts"
import type { Preview } from '@storybook/[your-framework]';
import type { NewBadgesConfig } from 'storybook-addon-badges';

const preview: Preview = {
//...other preview config
parameters: {
//...other parameters
badgesConfig: {
badgeMap: {
MyCustomBadge: {
locations: ['sidebar'],
title: "Custom"
}
},
locations: ['toolbar'],
// ...other addon config parameters
} satisfies NewBadgesConfig
}
}

export default preview;
```
67 changes: 67 additions & 0 deletions docs/docs/customisation/configuration.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
---
sidebar_position: 1
title: Configuration
---

# Configuration

To configure `storybook-addon-badges`, you will use storybook's [parameters](https://storybook.js.org/docs/writing-stories/parameters). Badges uses 2 parameter keys:

- `badges` - Contains a simple string array of the badges to display.
- `badgesConfig` - Contains configuration for the badge styles, and options for the plugin

## BadgesConfig

The `badgesConfig` parameter contains configuration options for the addon. It is recommended that you define this once in the [global parameters](https://storybook.js.org/docs/writing-stories/parameters#global-parameters), however this can be applied at the component and story level as well, to override the global values.

:::warning
Due to how Storybook's [inheritance](https://storybook.js.org/docs/writing-stories/parameters#rules-of-parameter-inheritance) works (objects are merged, array/boolean/number/string values are overridden):

- The values of `excludeTags`, `locations` and `useTags` will be completely overridden for that component/story, so you will need to define the complete value.
- The values for `badgeMap` and `baseStyle` will be merged (keys will only be added/overridden, not dropped), if you need to remove a value, you will need to explicitly set it to `undefined`.
:::

The `badgesConfig` offers the following options:

| Option | Description | Docs |
| ------------- | -------------------------------------------------------------------------------------------------------- | -------------------------------------------------- |
| `badgeMap` | A mapping object for providing custom badge configs. | [Create Custom Badges](./create-custom-badges.mdx) |
| `baseStyle` | The default style to use for badges | [Adding Styles](./adding-styles.mdx) |
| `excludeTags` | The storybook tags to prevent from creating badges. | [Using Tags](./using-tags.mdx) |
| `locations` | Default locations for badges to display | [Badge Locations](./badge-locations.mdx) |
| `useTags` | Whether to use storybook's [tags](https://storybook.js.org/docs/writing-stories/tags) to generate badges | [Using Tags](./using-tags.mdx) |


:::tip Using Typescript?
You can get type completion/validation in parameters by using the `BadgesConfig` type for the `badgesConfig` parameter.

```ts title=".storybook/preview.ts"
import type { Preview } from '@storybook/[your-framework]';
import type { BadgesConfig } from 'storybook-addon-badges';

const preview: Preview = {
//...other preview config
parameters: {
//...other parameters
badges: ["wip"] as string[],
badgesConfig: {
badgeMap: {
MyCustomBadge: {
title: 'My Custom Badge',
}
}
} satisfies BadgesConfig;
}
}

export default preview;
```
:::

## Badges

The `badges` key is a simple array of badge names to apply. Due to how Storybook's parameters work, these will be overwritten by `badges` parameters at a higher specificity. For example:

- `badges` defined in [global parameters](https://storybook.js.org/docs/writing-stories/parameters#global-parameters) will apply to any component/story that does not define its own badges at the component/story level.
- `badges` defined in [component parameters]](https://storybook.js.org/docs/writing-stories/parameters#component-parameters) will override global values, and apply to any story for that component that does not define its own `badges`.
- `badges` defined in [story parameters](https://storybook.js.org/docs/writing-stories/parameters#story-parameters) will override any global or story `badges`.
Loading

0 comments on commit 8f79051

Please sign in to comment.