mirror of
https://github.com/zhigang1992/react.git
synced 2026-02-02 09:08:52 +08:00
70
components/badge/badge.tsx
Normal file
70
components/badge/badge.tsx
Normal 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)
|
||||
3
components/badge/index.ts
Normal file
3
components/badge/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import Badge from './badge'
|
||||
|
||||
export default Badge
|
||||
@@ -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'
|
||||
|
||||
@@ -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>
|
||||
</>
|
||||
)
|
||||
|
||||
60
pages/docs/components/badge.mdx
Normal file
60
pages/docs/components/badge.mdx
Normal 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>
|
||||
Reference in New Issue
Block a user