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(elements): add spotlighted number to icon button #1292

Merged
merged 2 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
71 changes: 67 additions & 4 deletions src/elements/IconButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ const ICON_SIZE_IN_PX: Record<Size, number> = {
export type IconButtonProps = Omit<ButtonHTMLAttributes<HTMLButtonElement>, 'children'> & {
Icon: FunctionComponent<IconProps>
accent?: Accent | undefined
badge?: string | undefined
badgeBackgroundColor?: string | undefined
badgeColor?: string | undefined
badgeNumber?: number | undefined
color?: string | undefined
/** In pixels, override `size` prop default values. */
iconSize?: number | undefined
Expand All @@ -28,6 +30,9 @@ export type IconButtonProps = Omit<ButtonHTMLAttributes<HTMLButtonElement>, 'chi
}
export function IconButton({
accent = Accent.PRIMARY,
badgeBackgroundColor,
badgeColor,
badgeNumber,
className,
color,
Icon,
Expand Down Expand Up @@ -72,13 +77,40 @@ export function IconButton({

switch (accent) {
case Accent.SECONDARY:
return <SecondaryButton as={StyledButton} {...commonProps} />
return (
<Wrapper>
{!!badgeNumber && (
<BadgeNumber backgroundColor={badgeBackgroundColor} color={badgeColor} size={size}>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

je vais faire ma relou mais il me semble qu'on s'était dit que les props css on les préfixaient avec $, genre $backgroundColor (désolée j'ai pas fait attention avant)

{badgeNumber}
</BadgeNumber>
)}
<SecondaryButton as={StyledButton} {...commonProps} />
</Wrapper>
)

case Accent.TERTIARY:
return <TertiaryButton as={StyledButton} {...commonProps} />
return (
<Wrapper>
{!!badgeNumber && (
<BadgeNumber backgroundColor={badgeBackgroundColor} color={badgeColor} size={size}>
{badgeNumber}
</BadgeNumber>
)}
<TertiaryButton as={StyledButton} {...commonProps} />
</Wrapper>
)

default:
return <PrimaryButton as={StyledButton} {...commonProps} />
return (
<Wrapper>
{!!badgeNumber && (
<BadgeNumber backgroundColor={badgeBackgroundColor} color={badgeColor} size={size}>
{badgeNumber}
</BadgeNumber>
)}
<PrimaryButton as={StyledButton} {...commonProps} />
</Wrapper>
)
}
}

Expand All @@ -88,6 +120,37 @@ const PADDING: Record<Size, string> = {
[Size.SMALL]: '3px'
}

const Wrapper = styled.div`
position: relative;
`

const LEFT_MARGIN: Record<Size, number> = {
[Size.LARGE]: 35,
[Size.NORMAL]: 25,
[Size.SMALL]: 15
}

const BadgeNumber = styled.div<{
backgroundColor: string | undefined
color: string | undefined
size: Size
}>`
display: inline-block;
position: absolute;
height: 15px;
padding: 0 4px;
text-align: center;
border-radius: 10px;
top: -5px;
line-height: 14px;
left: ${p => (p.size ? LEFT_MARGIN[p.size] : 25)}px;
background: ${p => (p.backgroundColor ? p.backgroundColor : p.theme.color.maximumRed)};
color: ${p => (p.color ? p.color : p.theme.color.white)};
font-size: 12px;
letter-spacing: 0px;
font-weight: 700;
`

// We can't use $-prefixed props here for some reason (maybe because the `as` prop exclude them?).
const StyledButton = styled.button<{
isCompact: boolean | undefined
Expand Down
15 changes: 14 additions & 1 deletion stories/elements/IconButton.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Showcase } from '../../.storybook/components/Showcase'
import { ARG_TYPE, META_DEFAULTS } from '../../.storybook/constants'
import { generateStoryDecorator } from '../../.storybook/utils/generateStoryDecorator'
import { Accent, IconButton, Icon, Size } from '../../src'
import { THEME } from '../../src/theme'

import type { IconButtonProps } from '../../src'
import type { Meta } from '@storybook/react'
Expand All @@ -16,12 +17,15 @@ const meta: Meta<IconButtonProps> = {
argTypes: {
accent: ARG_TYPE.OPTIONAL_ACCENT,
color: ARG_TYPE.OPTIONAL_COLOR,
badgeBackgroundColor: ARG_TYPE.OPTIONAL_COLOR,
badgeColor: ARG_TYPE.OPTIONAL_COLOR,
disabled: ARG_TYPE.OPTIONAL_BOOLEAN,
Icon: ARG_TYPE.ICON,
iconSize: ARG_TYPE.OPTIONAL_NUMBER,
isCompact: ARG_TYPE.OPTIONAL_BOOLEAN,
size: ARG_TYPE.OPTIONAL_SIZE,
type: ARG_TYPE.NO_CONTROL,
badgeNumber: ARG_TYPE.OPTIONAL_NUMBER,
withUnpropagatedClick: ARG_TYPE.OPTIONAL_BOOLEAN
},

Expand All @@ -30,6 +34,9 @@ const meta: Meta<IconButtonProps> = {
disabled: false,
Icon: Icon.Close,
isCompact: false,
badgeNumber: 2,
badgeBackgroundColor: THEME.color.lightGray,
badgeColor: THEME.color.slateGray,
size: undefined,
withUnpropagatedClick: false
},
Expand Down Expand Up @@ -73,7 +80,13 @@ function ShowcaseReference() {
<IconButton accent={Accent.PRIMARY} className="" Icon={Icon.Search} size={Size.LARGE} />
</td>
<td>
<IconButton accent={Accent.PRIMARY} className="" Icon={Icon.Search} size={Size.NORMAL} />
<IconButton
accent={Accent.PRIMARY}
badgeNumber={123}
className=""
Icon={Icon.Search}
size={Size.NORMAL}
/>
</td>
<td>
<IconButton accent={Accent.PRIMARY} className="" Icon={Icon.Search} size={Size.SMALL} />
Expand Down
Loading