Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Badge component #1045

Merged
merged 19 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions packages/css/src/components/badge/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Badge

A prominently colored box containing 1 or 2 words to guide the user in taking a specific action.
dlnr marked this conversation as resolved.
Show resolved Hide resolved

## Design

The text in the Badge is of the same size as the surrounding text.
alimpens marked this conversation as resolved.
Show resolved Hide resolved
Its color is either white or black, depending on the background colour.
The box aligns to the left with the running text.
alimpens marked this conversation as resolved.
Show resolved Hide resolved

## Guidelines

The default background colour is green.
Suggestions when to use the other colours will follow soon.
58 changes: 58 additions & 0 deletions packages/css/src/components/badge/badge.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* @license EUPL-1.2+
* Copyright (c) 2023 Gemeente Amsterdam
*/

.amsterdam-badge {
display: inline-block;
font-family: var(--amsterdam-badge-font-family);
font-size: var(--amsterdam-badge-spacious-font-size);
font-weight: var(--amsterdam-badge-font-weight);
line-height: var(--amsterdam-badge-spacious-line-height);
padding-inline: var(--amsterdam-badge-padding-inline);

.amsterdam-theme--compact & {
font-size: var(--amsterdam-badge-compact-font-size);
line-height: var(--amsterdam-badge-compact-line-height);
}
}

.amsterdam-badge--blue {
background-color: var(--amsterdam-badge-blue-background-color);
color: var(--amsterdam-badge-blue-color);
}

.amsterdam-badge--dark-blue {
background-color: var(--amsterdam-badge-dark-blue-background-color);
color: var(--amsterdam-badge-dark-blue-color);
VincentSmedinga marked this conversation as resolved.
Show resolved Hide resolved
}

.amsterdam-badge--green {
background-color: var(--amsterdam-badge-green-background-color);
color: var(--amsterdam-badge-green-color);
}

.amsterdam-badge--dark-green {
background-color: var(--amsterdam-badge-dark-green-background-color);
color: var(--amsterdam-badge-dark-green-color);
}

.amsterdam-badge--orange {
background-color: var(--amsterdam-badge-orange-background-color);
color: var(--amsterdam-badge-orange-color);
}

.amsterdam-badge--magenta {
background-color: var(--amsterdam-badge-magenta-background-color);
color: var(--amsterdam-badge-magenta-color);
}

.amsterdam-badge--purple {
background-color: var(--amsterdam-badge-purple-background-color);
color: var(--amsterdam-badge-purple-color);
}

.amsterdam-badge--yellow {
background-color: var(--amsterdam-badge-yellow-background-color);
color: var(--amsterdam-badge-yellow-color);
}
alimpens marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions packages/css/src/components/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

/* Append here */
@import "./badge/badge";
@import "./table/table";
@import "./mega-menu/mega-menu";
@import "./icon-button/icon-button";
Expand Down
59 changes: 59 additions & 0 deletions packages/react/src/Badge/Badge.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { render } from '@testing-library/react'
import { createRef } from 'react'
import { Badge } from './Badge'
import '@testing-library/jest-dom'

describe('Badge', () => {
it('renders', () => {
const { container } = render(<Badge />)

const component = container.querySelector(':only-child')

expect(component).toBeInTheDocument()
expect(component).toBeVisible()
})

it('renders a design system BEM class name', () => {
const { container } = render(<Badge />)

const component = container.querySelector(':only-child')

expect(component).toHaveClass('amsterdam-badge')
})

it('renders an additional class name', () => {
const { container } = render(<Badge className="extra" />)

const component = container.querySelector(':only-child')

expect(component).toHaveClass('amsterdam-badge extra')
})

it('supports ForwardRef in React', () => {
const ref = createRef<HTMLElement>()

const { container } = render(<Badge ref={ref} />)

const component = container.querySelector(':only-child')

expect(ref.current).toBe(component)
})

it('renders with default color', () => {
const { container } = render(<Badge />)

const component = container.querySelector(':only-child')

expect(component).toHaveClass('amsterdam-badge')
alimpens marked this conversation as resolved.
Show resolved Hide resolved
expect(component).toHaveClass('amsterdam-badge--dark-green')
})

it('renders with specified color', () => {
const { container } = render(<Badge color="blue" />)

const component = container.querySelector(':only-child')

expect(component).toHaveClass('amsterdam-badge')
alimpens marked this conversation as resolved.
Show resolved Hide resolved
expect(component).toHaveClass('amsterdam-badge--blue')
})
alimpens marked this conversation as resolved.
Show resolved Hide resolved
})
22 changes: 22 additions & 0 deletions packages/react/src/Badge/Badge.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* @license EUPL-1.2+
* Copyright (c) 2023 Gemeente Amsterdam
*/

import clsx from 'clsx'
import { forwardRef } from 'react'
import type { ForwardedRef, HTMLAttributes, PropsWithChildren } from 'react'

export interface BadgeProps extends PropsWithChildren<HTMLAttributes<HTMLElement>> {
color?: 'blue' | 'dark-blue' | 'green' | 'dark-green' | 'orange' | 'magenta' | 'purple' | 'yellow'
}

export const Badge = forwardRef(
({ children, className, color = 'dark-green', ...restProps }: BadgeProps, ref: ForwardedRef<HTMLElement>) => (
<span {...restProps} ref={ref} className={clsx('amsterdam-badge', `amsterdam-badge--${color}`, className)}>
{children}
VincentSmedinga marked this conversation as resolved.
Show resolved Hide resolved
</span>
),
)

Badge.displayName = 'Badge'
3 changes: 3 additions & 0 deletions packages/react/src/Badge/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# React Badge component

[Badge documentation](../../../css/src/badge/README.md)
2 changes: 2 additions & 0 deletions packages/react/src/Badge/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { Badge } from './Badge'
export type { BadgeProps } from './Badge'
1 change: 1 addition & 0 deletions packages/react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

/* Append here */
export * from './Badge'
export * from './Table'
export * from './MegaMenu'
export * from './IconButton'
Expand Down
49 changes: 49 additions & 0 deletions proprietary/tokens/src/components/amsterdam/badge.tokens.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"amsterdam": {
"badge": {
"font-family": { "value": "{amsterdam.typography.font-family}" },
"font-weight": { "value": "{amsterdam.typography.font-weight.bold}" },
"padding-inline": { "value": "0.5rem" },
"spacious": {
"font-size": { "value": "{amsterdam.typography.spacious.text-level.5.font-size}" },
"line-height": { "value": "{amsterdam.typography.spacious.text-level.5.line-height}" }
},
"compact": {
"font-size": { "value": "{amsterdam.typography.compact.text-level.5.font-size}" },
"line-height": { "value": "{amsterdam.typography.compact.text-level.5.line-height}" }
},
"dark-blue": {
"background-color": { "value": "{amsterdam.color.dark-blue}" },
"color": { "value": "{amsterdam.color.primary-white}" }
},
"blue": {
"background-color": { "value": "{amsterdam.color.blue}" },
"color": { "value": "{amsterdam.color.primary-white}" }
},
"green": {
"background-color": { "value": "{amsterdam.color.green}" },
"color": { "value": "{amsterdam.color.primary-black}" }
},
"dark-green": {
"background-color": { "value": "{amsterdam.color.dark-green}" },
"color": { "value": "{amsterdam.color.primary-white}" }
},
"orange": {
"background-color": { "value": "{amsterdam.color.orange}" },
"color": { "value": "{amsterdam.color.primary-black}" }
},
"magenta": {
"background-color": { "value": "{amsterdam.color.magenta}" },
"color": { "value": "{amsterdam.color.primary-white}" }
},
"purple": {
"background-color": { "value": "{amsterdam.color.purple}" },
"color": { "value": "{amsterdam.color.primary-white}" }
},
"yellow": {
"background-color": { "value": "{amsterdam.color.yellow}" },
"color": { "value": "{amsterdam.color.primary-black}" }
}
}
}
}
19 changes: 19 additions & 0 deletions storybook/storybook-react/src/Badge/Badge.docs.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Canvas, Markdown, Meta, Primary } from "@storybook/blocks";
import * as BadgeStories from "./Badge.stories.tsx";
import README from "../../../../packages/css/src/components/badge/README.md?raw";

<Meta of={BadgeStories} />

<Markdown>{README}</Markdown>

## Voorbeelden
alimpens marked this conversation as resolved.
Show resolved Hide resolved

### Default

<Primary />

alimpens marked this conversation as resolved.
Show resolved Hide resolved
### In a list

A tip can be used in a list.

<Canvas of={BadgeStories.InList} />
alimpens marked this conversation as resolved.
Show resolved Hide resolved
53 changes: 53 additions & 0 deletions storybook/storybook-react/src/Badge/Badge.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* @license EUPL-1.2+
* Copyright (c) 2023 Gemeente Amsterdam
*/

import { Badge, Heading, OrderedList } from '@amsterdam/design-system-react'
import { Meta, StoryObj } from '@storybook/react'

const meta = {
title: 'Feedback/Badge',
component: Badge,
args: {
children: 'Tip',
},
argTypes: {
color: {
control: {
type: 'inline-radio',
VincentSmedinga marked this conversation as resolved.
Show resolved Hide resolved
},
options: ['blue', 'dark-blue', 'green', 'dark-green', 'orange', 'magenta', 'purple', 'yellow'],
selected: 'dark-green',
},
},
} satisfies Meta<typeof Badge>

export default meta

type Story = StoryObj<typeof meta>

export const Default: Story = {}
VincentSmedinga marked this conversation as resolved.
Show resolved Hide resolved

export const InList: Story = {
decorators: [
(Story) => (
<div>
<Heading level={4}>Verwerkingstijd en data</Heading>
<OrderedList>
<OrderedList.Item>Binnen Amsterdam op werkdagen binnen 24 uur.</OrderedList.Item>
<OrderedList.Item>
Vanuit een andere gemeente ongeveer 5 werkdagen.
<br />
<Story /> Geeft u uw verhuizing vantevoren door, dan wordt uw melding bewaard tot de verhuisdatum. Dan pas
begint de termijn van 24 uur of 5 werkdagen.
</OrderedList.Item>
<OrderedList.Item>
Een verhuizing doorgeven kan 4 weken van tevoren en maximaal 5 dagen erna. Geeft u het later door? Dan wordt
de dag van doorgeven de verhuisdatum.
</OrderedList.Item>
</OrderedList>
</div>
),
],
}