import { ReactNode } from 'react'; import styled from '@emotion/styled'; import { css } from '@emotion/react'; import { rgba } from 'polished'; import { primary, valencia, nobel, white, mineShaft } from 'styles/colors'; import { fontSize, getThemeFont } from 'styles/fonts'; const Container = styled.div>` position: relative; display: inline-flex; box-sizing: border-box; flex: 1; flex-flow: row nowrap; border: 1px solid ${nobel.main}; border-radius: 2px; font-family: ${getThemeFont}; width: 100%; ${({ theme: { rtl } }) => css` direction: ${rtl ? 'rtl' : 'ltr'}; text-align: ${rtl ? 'right' : 'left'}; `}; ${({ disabled }) => disabled && ` opacity: 0.5; cursor: not-allowed; `}; ${({ focused, error }) => focused && ` border: 1px solid ${primary.main}; box-shadow: 0 0 0 4px ${ error ? rgba(valencia.main, 0.2) : rgba(primary.main, 0.2) }; `}; ${({ error }) => error && ` border: 1px solid ${valencia.main}; background-color: ${rgba(valencia.main, 0.1)}; `}; `; const Label = styled.label< Pick >` position: absolute; top: 50%; left: ${({ prefixWidth }) => (prefixWidth ? `${prefixWidth}px` : '1em')}; color: ${mineShaft[600]}; font-family: ${getThemeFont}; font-size: ${fontSize.regular}; line-height: 1; white-space: nowrap; pointer-events: none; transition: 0.15s; transform-origin: left center; transform: translateY(-50%); ${({ required }) => required && css` :after { content: ' *'; color: ${valencia.main}; } `}; ${({ dirty }) => dirty && css` color: ${mineShaft[500]}; `}; ${({ focused }) => focused && css` color: ${primary.main}; `}; ${({ error }) => error && css` color: ${valencia.main}; `}; ${({ focused, dirty, error, theme: { rtl } }) => (focused || dirty || error) && css` top: 0; left: 0.5em; padding: 0 4px; background-color: ${white}; transform: translateY(-50%) scale(0.7); ${rtl && css` left: auto; right: 0.5em; transform-origin: right center; `} `}; ${({ dirty, theme: { rtl }, focused }) => !(dirty || focused) && rtl && css` { left: auto; right: 1em; } `} `; interface Props { /** Children elements, HTML elements or `React.ReactElement` are allowed */ children?: ReactNode; /** Status of label text position, text will be placed at top left position if `true` */ dirty?: boolean; /** Status of error border, red border will be applied if `true` */ error?: boolean; /** Status of focused border, orange border will be applied if `true` */ focused?: boolean; /** True if disable interaction with the element, applying opacity to the border */ disabled?: boolean; /** Label text that will be shown on border / inside the border */ label?: string; /** Name that used to reference the children and label text */ name?: string; /** Width of the prefix elements */ prefixWidth?: number; /** Shows a red asterisk next to the label if true */ required?: boolean; /** Props passed to component wrapper */ [rest: string]: any; } interface PlaceholderProps { dirty?: boolean; error?: boolean; focused?: boolean; htmlFor?: string; title?: string | null; prefixWidth?: number; required?: boolean; } const Placeholder = ({ dirty = false, error = false, focused = false, htmlFor, title = null, prefixWidth = 0, required = false, }: PlaceholderProps) => ( ); const AnimatedBorder = ({ children = null, dirty = false, error = false, focused = false, disabled = false, label = '', name = '', prefixWidth = 0, required = false, ...rest }: Props) => ( {label && ( )} {children} ); export default AnimatedBorder;