Skip to content

Commit

Permalink
chore #175 - Extract out IconButton from WAM
Browse files Browse the repository at this point in the history
  • Loading branch information
sam-dassana committed Dec 11, 2020
1 parent c52d2d5 commit 5ca85ae
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
87 changes: 87 additions & 0 deletions src/components/IconButton/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import cn from 'classnames'
import { createUseStyles } from 'react-jss'
import { faTimes } from '@fortawesome/free-solid-svg-icons'
import { generateThemedIconBtnStyles } from './utils'
import {
FontAwesomeIcon,
FontAwesomeIconProps
} from '@fortawesome/react-fontawesome'
import React, { FC, SyntheticEvent } from 'react'
import { styleguide, ThemeType } from 'components/assets/styles'

const { borderRadius, flexCenter, font, spacing } = styleguide
const { light, dark } = ThemeType

const useStyles = createUseStyles({
hasBorder: {
...flexCenter,
border: '1px solid',
borderRadius,
height: spacing.xl,
width: spacing.xl
},
icon: {
fontSize: props => (props.size ? props.size : 'inherit')
},
iconButton: {
...generateThemedIconBtnStyles(light),
cursor: 'pointer',
lineHeight: 0,
transition: 'border-color 0.3s, color 0.3s'
},
// eslint-disable-next-line sort-keys
'@global': {
[`.${dark}`]: {
'& $iconButton': generateThemedIconBtnStyles(dark)
}
}
})

export enum IconSizes {
'xs' = font.label.fontSize,
'sm' = font.body.fontSize,
'lg' = font.h2.fontSize
}

interface Props {
classes?: string[]
hasBorder?: boolean
icon?: FontAwesomeIconProps['icon']
onClick?: (e?: SyntheticEvent) => void
size?: number
}

/* NOTE: This will be moved to web-components eventually */
export const IconButton: FC<Props> = ({
classes = [],
hasBorder = false,
icon = faTimes,
onClick,
size
}: Props) => {
const componentClasses = useStyles({ size })

const iconBtnClasses = cn(
{
[componentClasses.iconButton]: true,
[componentClasses.hasBorder]: hasBorder
},
classes
)

const optionalProps: Pick<Props, 'onClick'> = {}

if (onClick) {
optionalProps.onClick = onClick
}

return (
<span className={iconBtnClasses}>
<FontAwesomeIcon
className={componentClasses.icon}
icon={icon}
{...optionalProps}
/>
</span>
)
}
34 changes: 34 additions & 0 deletions src/components/IconButton/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { styleguide, themedStyles, ThemeType } from 'components/assets/styles'

const {
colors: { blacks }
} = styleguide

const { light, dark } = ThemeType

const iconBtnPalette = {
[dark]: {
hoverBorderColor: blacks['lighten-40']
},
[light]: {
hoverBorderColor: blacks['lighten-50']
}
}

export const generateThemedIconBtnStyles = (themeType: ThemeType) => {
const {
base: { borderColor, color },
hover
} = themedStyles[themeType]

const { hoverBorderColor } = iconBtnPalette[themeType]

return {
'&:hover': {
borderColor: hoverBorderColor,
color: hover.color
},
borderColor,
color
}
}

0 comments on commit 5ca85ae

Please sign in to comment.