|
| 1 | +import { Meta, Story } from '@storybook/addon-docs/blocks'; |
| 2 | +import TopBarStories, { |
| 3 | + CssOverrides, |
| 4 | + CustomTheme, |
| 5 | + Default, |
| 6 | +} from './TopBar.stories'; |
| 7 | +import { |
| 8 | + SandboxReact, |
| 9 | + SandboxCss, |
| 10 | + SandboxJs, |
| 11 | +} from '../../util/storybook/sandbox/Sandbox'; |
| 12 | +import { |
| 13 | + componentName, |
| 14 | + componentCss, |
| 15 | + componentHtml, |
| 16 | + componentJs, |
| 17 | + componentTsx, |
| 18 | +} from './sandbox'; |
| 19 | + |
| 20 | +<Meta of={TopBarStories} /> |
| 21 | + |
| 22 | +# TopBar |
| 23 | + |
| 24 | +A top bar that provides information about the tool, the current state and navigation links among other things. |
| 25 | + |
| 26 | +## When to use |
| 27 | + |
| 28 | +- At the top of a tool |
| 29 | +- To identify the tool, its current state and the user |
| 30 | +- To navigate around the tool |
| 31 | + |
| 32 | +## Peer dependencies |
| 33 | + |
| 34 | +- `@emotion/react` |
| 35 | +- `react` |
| 36 | +- `react-dom` |
| 37 | +- `typescript` |
| 38 | + |
| 39 | +See the `peerDependencies` section of `package.json` for compatible versions. |
| 40 | + |
| 41 | +See [custom component build](#custom-component-build) for usage without React/Emotion. |
| 42 | + |
| 43 | +## Example usage |
| 44 | + |
| 45 | +<SandboxReact componentName={componentName} componentTsx={componentTsx} /> |
| 46 | + |
| 47 | +```tsx |
| 48 | +import { TopBar, TopBarLHS, TopBarItem } from '@guardian/stand/TopBar'; |
| 49 | + |
| 50 | +/* types, if required */ |
| 51 | +import type { TopBarProps, TopBarTheme } from '@guardian/stand/TopBar'; |
| 52 | + |
| 53 | +<TopBar theme={customTheme}> |
| 54 | + <TopBarToolName name="Author" favicon={{ letter: 'A' }} /> |
| 55 | + <TopBarLHS> |
| 56 | + <TopBarItem>Hello</TopBarItem> |
| 57 | + </TopBarLHS> |
| 58 | + <TopBarRHS> |
| 59 | + <TopBarNavigation text={'Link to somewhere'} href="#" /> |
| 60 | + </TopBarRHS> |
| 61 | + <Avatar |
| 62 | + src="https://uploads.guimcode.co.uk/2026/01/27/f85e2e477ce54f4c3b671faa5cd21673aa9f8072fddb5d70a73e6038dc812eec.jpg" |
| 63 | + alt="Mahesh Makani" |
| 64 | + size="sm" |
| 65 | + /> |
| 66 | +</TopBar>; |
| 67 | +``` |
| 68 | + |
| 69 | +## Composition |
| 70 | + |
| 71 | +The `TopBar` component is composed of several sub-components. Only children of type `TopBarToolName`, `Avatar`, `TopBarLHS` or `TopBarRHS` are valid. |
| 72 | + |
| 73 | +The children will be rendered in the following order from left to right: |
| 74 | + |
| 75 | +| `TopBarToolName` | `TopBarLHS` | `TopBarRHS` | `Avatar` | |
| 76 | + |
| 77 | +`TopBarToolName` will always be on the left and `Avatar` on the right. |
| 78 | + |
| 79 | +### Props |
| 80 | + |
| 81 | +| Name | Type | Required | Default | Description | |
| 82 | +| -------------- | ------------------ | -------- | ------- | -------------------------------------------------------------------------------------------------------------------------------------- | |
| 83 | +| `children` | `React.ReactNode` | No | N/A | Content to render inside the component. Only children of type `TopBarToolName`, `Avatar`, `TopBarLHS` or `TopBarRHS` will be rendered. | |
| 84 | +| `theme` | `TopBarTheme` | No | N/A | Custom theme overrides. | |
| 85 | +| `cssOverrides` | `SerializedStyles` | No | N/A | Custom CSS styles. | |
| 86 | +| `className` | `string` | No | N/A | Additional class name(s). | |
| 87 | + |
| 88 | +### `TopBarRHS` / `TopBarRHS` props |
| 89 | + |
| 90 | +All other content must be composed within the left hand side or right hand side. The only valid children are [TopBarNavigation](/docs/stand-tools-design-system-components-topbar-topbarnavigation--docs) and [TopBarItem](/docs/stand-tools-design-system-components-topbar-topbaritem--docs). |
| 91 | + |
| 92 | +`TopBarNavigation` children should be used for navigation links around the tool. `TopBarItem` provides the necessary styling for any other child components in the top bar. |
| 93 | + |
| 94 | +| Name | Type | Required | Default | Description | |
| 95 | +| ---------- | ----------------- | -------- | ------- | ------------------------------------------------------------------------------------------------------------------------ | |
| 96 | +| `children` | `React.ReactNode` | No | N/A | Content to render on one side of the top bar. Only children of type `TopBarItem` or `TopBarNavigation` will be rendered. | |
| 97 | + |
| 98 | +## Stories |
| 99 | + |
| 100 | +### Default |
| 101 | + |
| 102 | +<Story of={Default} /> |
| 103 | + |
| 104 | +## Customisation |
| 105 | + |
| 106 | +We recommend using the TopBar component as provided, but it can be customised using the `theme` or `cssOverrides` props as required. |
| 107 | + |
| 108 | +### Custom theme |
| 109 | + |
| 110 | +The `theme` prop allows you to override specific design tokens for the TopBar component: |
| 111 | + |
| 112 | +```tsx |
| 113 | +import type { TopBarTheme } from '@guardian/stand/TopBar'; |
| 114 | +import { TopBar, TopBarLHS, TopBarItem } from '@guardian/stand/TopBar'; |
| 115 | +import { baseColors } from '@guardian/stand'; |
| 116 | + |
| 117 | +const customTheme: Partial<TopBarTheme> = { |
| 118 | + border: `5px solid ${baseColors['cool-purple']['700']}`, |
| 119 | +}; |
| 120 | + |
| 121 | +const Component = () => ( |
| 122 | + <TopBar theme={customTheme}> |
| 123 | + <TopBarLHS> |
| 124 | + <TopBarToolName name="Custom" favicon={{ letter: 'C' }} /> |
| 125 | + <TopBarItem>Hello</TopBarItem> |
| 126 | + </TopBarLHS> |
| 127 | + </TopBar> |
| 128 | +); |
| 129 | +``` |
| 130 | + |
| 131 | +<Story of={CustomTheme} /> |
| 132 | + |
| 133 | +### CSS overrides |
| 134 | + |
| 135 | +The `cssOverrides` prop allows you to pass custom CSS to the TopBar component: |
| 136 | + |
| 137 | +```tsx |
| 138 | +import { TopBar } from '@guardian/stand/TopBar'; |
| 139 | +import { baseColors } from '@guardian/stand'; |
| 140 | +import { css } from '@emotion/react'; |
| 141 | + |
| 142 | +const customStyles = css\` |
| 143 | + border: 4px solid ${baseColors.cyan['300']}; |
| 144 | +\`; |
| 145 | +
|
| 146 | +const Component = () => |
| 147 | + <TopBar cssOverrides={customStyles}> |
| 148 | + <TopBarToolName name="CssOverides" favicon={{ letter: 'C' }} /> |
| 149 | + <TopBarLHS> |
| 150 | + <TopBarItem>Hello</TopBarItem> |
| 151 | + </TopBarLHS> |
| 152 | + </TopBar> |
| 153 | +; |
| 154 | +``` |
| 155 | + |
| 156 | +<Story of={CssOverrides} /> |
| 157 | + |
| 158 | +## Custom Component Build |
| 159 | + |
| 160 | +If you're not using React/Emotion, you can create a custom TopBar component using the styles defined in the `TopBarTheme` type. |
| 161 | + |
| 162 | +**`css`** |
| 163 | + |
| 164 | +You can import the TopBar styles as CSS from the package: |
| 165 | + |
| 166 | +<SandboxCss |
| 167 | + componentName={componentName} |
| 168 | + componentHtml={componentHtml} |
| 169 | + componentCss={componentCss} |
| 170 | +/> |
| 171 | + |
| 172 | +**TypeScript/JavaScript** |
| 173 | + |
| 174 | +Use the `componentTopBar` variable and the `ComponentTopBar` type: |
| 175 | + |
| 176 | +<SandboxJs componentName={componentName} componentJs={componentJs} /> |
0 commit comments