Merge pull request #27 from unix/badge

feat: add badge component
This commit is contained in:
witt
2020-03-27 09:44:06 +08:00
committed by GitHub
5 changed files with 139 additions and 5 deletions

View File

@@ -0,0 +1,70 @@
import React, { useMemo } from 'react'
import withDefaults from '../utils/with-defaults'
import useTheme from '../styles/use-theme'
import { NormalSizes, NormalTypes } from '../utils/prop-types'
import { ZeitUIThemesPalette } from 'components/styles/themes'
interface Props {
type?: NormalTypes
size?: NormalSizes
className?: string
}
const defaultProps = {
type: 'default' as NormalTypes,
size: 'medium' as NormalSizes,
className: '',
}
export type BadgeProps = Props & typeof defaultProps & React.HTMLAttributes<any>
const getFontSize = (size: NormalSizes) => {
const sizes: { [key in NormalSizes]: string } = {
mini: '.7rem',
small: '.75rem',
medium: '.875rem',
large: '1rem',
}
return sizes[size]
}
const getBgColor = (type: NormalTypes, palette: ZeitUIThemesPalette) => {
const colors: { [key in NormalTypes]: string } = {
default: palette.foreground,
success: palette.success,
warning: palette.warning,
error: palette.error,
secondary: palette.secondary,
}
return colors[type]
}
const Badge: React.FC<React.PropsWithChildren<BadgeProps>> = React.memo(({
type, size, className, children, ...props
}) => {
const theme = useTheme()
const bg = useMemo(() => getBgColor(type, theme.palette), [type, theme.palette])
const font = useMemo(() => getFontSize(size), [size])
return (
<span className={className} {...props}>
{children}
<style jsx>{`
span {
display: inline-block;
padding: 4px 7px;
border-radius: 16px;
font-variant: tabular-nums;
line-height: 1;
vertical-align: middle;
background-color: ${bg};
color: white;
font-size: ${font};
border: 0;
}
`}</style>
</span>
)
})
export default withDefaults(Badge, defaultProps)

View File

@@ -0,0 +1,3 @@
import Badge from './badge'
export default Badge

View File

@@ -36,3 +36,4 @@ export { default as Select } from './select'
export { default as Tabs } from './tabs'
export { default as Progress } from './progress'
export { default as Tree } from './file-tree'
export { default as Badge } from './badge'

View File

@@ -155,11 +155,6 @@ const CSSBaseline: React.FC<React.PropsWithChildren<{}>> = React.memo(({
font-weight: 600;
}
selection {
background-color: ${theme.palette.background};
color: ${theme.palette.foreground};
}
input-webkit-autofill {
box-shadow: 0 0 0 100px var(--geist-background) inset;
}
@@ -240,6 +235,11 @@ const CSSBaseline: React.FC<React.PropsWithChildren<{}>> = React.memo(({
outline: none;
list-style: none;
}
::selection {
background-color: ${theme.palette.selection};
color: ${theme.palette.foreground};
}
`}</style>
</>
)

View File

@@ -0,0 +1,60 @@
import { Layout, Playground, Attributes } from 'lib/components'
import { Badge, Spacer } from 'components'
export const meta = {
title: 'Badge',
description: 'Badge',
}
## Badge
Display an indicator that requires attention.
<Playground
scope={{ Badge, Spacer }}
code={`
<>
<Badge>1</Badge> <Spacer y={.5} />
<Badge>50</Badge> <Spacer y={.5} />
<Badge>100</Badge> <Spacer y={.5} />
<Badge>2020</Badge>
</>
`} />
<Playground
title="Type"
scope={{ Badge, Spacer }}
code={`
<>
<Badge type="success">Success</Badge> <Spacer y={.5} />
<Badge type="warning">Warning</Badge> <Spacer y={.5} />
<Badge type="error">Error</Badge> <Spacer y={.5} />
<Badge type="secondary">Secondary</Badge>
</>
`} />
<Playground
title="Size"
scope={{ Badge, Spacer }}
code={`
<>
<Badge size="mini">Mini</Badge> <Spacer y={.5} />
<Badge size="small">Small</Badge> <Spacer y={.5} />
<Badge size="medium">Medium</Badge> <Spacer y={.5} />
<Badge size="large">Large</Badge>
</>
`} />
<Attributes edit="/pages/docs/components/badge.mdx">
<Attributes.Title>Badge.Props</Attributes.Title>
| Attribute | Description | Type | Accepted values | Default
| ---------- | ---------- | ---- | -------------- | ------ |
| **type** | badge type | `NormalTypes` | `'default', 'secondary', 'success', 'warning', 'error'` | `default` |
| **size** | badge size | `NormalSizes` | `'mini', 'small', 'medium', 'large'` | `medium` |
| ... | native props | `HTMLAttributes` | `'alt', 'id', 'className', ...` | - |
</Attributes>
export default ({ children }) => <Layout meta={meta}>{children}</Layout>