Skip to content

Instantly share code, notes, and snippets.

@alobato
Created June 8, 2022 11:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alobato/00766c6223260d7da26f05efa6dfddf4 to your computer and use it in GitHub Desktop.
Save alobato/00766c6223260d7da26f05efa6dfddf4 to your computer and use it in GitHub Desktop.
MaskedInput.js
import React from 'react'
import { css as transformStyleObject, get } from '@theme-ui/css'
import { useTheme } from '@emotion/react'
import { css as createClassName } from '@emotion/css'
import { IMaskInput } from 'react-imask'
const MaskedInput = React.forwardRef(({ __themeKey = 'forms', __css, variant = 'input', sx, style, ...rest }, ref) => {
const theme = useTheme()
const autofillStyles = {
boxShadow: 'inset 0 0 0 1000px var(--theme-ui-input-autofill-bg)',
fontSize: 'inherit',
':first-line': {
fontSize: '1rem',
}
}
const baseStyles = {
display: 'block',
width: '100%',
p: 2,
appearance: 'none',
fontSize: 'inherit',
lineHeight: 'inherit',
border: '1px solid',
borderRadius: 4,
color: 'inherit',
bg: 'transparent',
':autofill, :autofill:hover, :autofill:focus': autofillStyles,
':-webkit-autofill, :-webkit-autofill:hover, :-webkit-autofill:focus': autofillStyles
}
const __cssStyles = transformStyleObject(__css)(theme)
const variantInTheme = get(theme, `${__themeKey}.${variant}`) || get(theme, variant)
const variantStyles = variantInTheme && transformStyleObject(variantInTheme)(theme)
const sxPropStyles = transformStyleObject(sx)(theme)
const emotionStyle = {
...baseStyles,
...__cssStyles,
...variantStyles,
...sxPropStyles
}
const className = createClassName(emotionStyle)
return (
<IMaskInput ref={ref} className={className} style={style} {...rest} />
)
})
export default MaskedInput
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment