-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
137 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import * as React from 'react' | ||
import styled, { css } from 'styled-components' | ||
|
||
import { InjectedProps } from '../../hocs/withTheme' | ||
|
||
import { hoverable } from '../../hocs/hoverable' | ||
import { isTouchDevice } from '../../libs/ua' | ||
|
||
type Tag = 'button' | 'a' | ||
type Size = 'default' | 's' | ||
|
||
interface BaseProps { | ||
size?: Size | ||
children?: React.ReactNode | ||
className?: string | ||
prefix?: React.ReactNode | ||
suffix?: React.ReactNode | ||
square?: boolean | ||
wide?: boolean | ||
} | ||
|
||
export const buttonFactory: <Props extends BaseProps>( | ||
tag: Tag, | ||
) => React.FC<Props & InjectedProps> = tag => ({ | ||
size = 'default', | ||
className = '', | ||
square = false, | ||
children = '', | ||
prefix = '', | ||
suffix = '', | ||
theme, | ||
...props | ||
}) => { | ||
const Tag = hoverable()(Base.withComponent(tag)) | ||
|
||
// prettier-ignore | ||
const classNames = `${size} ${className} ${square ? 'square' : ''} ${prefix ? 'prefix' : ''} ${suffix ? 'suffix' : ''}` | ||
|
||
return ( | ||
<Tag className={classNames} theme={theme} {...props}> | ||
{prefix && <Prefix theme={theme}>{prefix}</Prefix>} | ||
{children} | ||
{suffix && <Suffix theme={theme}>{suffix}</Suffix>} | ||
</Tag> | ||
) | ||
} | ||
|
||
const Base: any = styled.div` | ||
${({ theme }: InjectedProps) => { | ||
const { frame, size, interaction } = theme | ||
return css` | ||
display: inline-flex; | ||
justify-content: center; | ||
align-items: center; | ||
width: ${({ wide }: any) => (wide ? '100%;' : 'auto')}; | ||
min-width: 2rem; | ||
vertical-align: middle; | ||
border: none; | ||
border-radius: ${frame.border.radius.m}; | ||
color: #fff; | ||
font-family: inherit; | ||
font-weight: bold; | ||
text-align: center; | ||
text-decoration: none; | ||
box-sizing: border-box; | ||
cursor: pointer; | ||
transition: ${isTouchDevice ? 'none' : `all ${interaction.hover.animation}`}; | ||
&.default { | ||
font-size: ${size.pxToRem(size.font.TALL)}; | ||
height: 40px; | ||
line-height: 40px; | ||
padding: 0 ${size.pxToRem(size.space.XS)}; | ||
} | ||
&.s { | ||
font-size: ${size.pxToRem(size.font.SHORT)}; | ||
height: 27px; | ||
line-height: 27px; | ||
padding: 0 ${size.pxToRem(size.space.XXS)}; | ||
} | ||
&.square { | ||
width: 40px; | ||
line-height: 40px; | ||
padding: 0; | ||
&.s { | ||
width: 27px; | ||
min-width: 27px; | ||
} | ||
} | ||
&[disabled] { | ||
cursor: not-allowed; | ||
} | ||
&.suffix { | ||
justify-content: space-between; | ||
} | ||
&.prefix { | ||
justify-content: left; | ||
} | ||
` | ||
}} | ||
` | ||
|
||
const Prefix = styled.span` | ||
${({ theme }: InjectedProps) => { | ||
const { size } = theme | ||
return css` | ||
display: inline-flex; | ||
margin-right: ${size.pxToRem(size.space.XXS)}; | ||
` | ||
}} | ||
` | ||
const Suffix = styled.span` | ||
${({ theme }: InjectedProps) => { | ||
const { size } = theme | ||
return css` | ||
display: inline-flex; | ||
margin-left: ${size.pxToRem(size.space.XXS)}; | ||
` | ||
}} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters