This repository has been archived by the owner on Sep 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(pseudobox): created pseudobox component
- Loading branch information
1 parent
8de50d4
commit 0660fbd
Showing
11 changed files
with
172 additions
and
51 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
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
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,19 @@ | ||
import styled from 'vue-styled-components' | ||
import css from '@styled-system/css' | ||
import { Box } from '../../lib/core/' | ||
import { pseudoProps } from '../../lib/config/props' | ||
import { parsePseudoStyles } from './utils' | ||
|
||
/** | ||
* The PseudoBox component is a wrapper for the Box component that allows us to provide pseudo styles for `_focus`, `_hover`, `_active`, etc. and `aria-*` attributes | ||
*/ | ||
const PseudoBox = styled(Box, { | ||
...pseudoProps | ||
})` | ||
${(props) => { | ||
const styles = parsePseudoStyles(props) | ||
return css(styles) | ||
} | ||
}` | ||
|
||
export default PseudoBox |
Empty file.
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,50 @@ | ||
import map from 'lodash-es/map' | ||
import { cleanProps, filterPseudo, tx } from '../../lib/utils/' | ||
|
||
/** | ||
* PseudoBox pseudo selectors | ||
*/ | ||
export const selectors = { | ||
_hover: '&:hover', | ||
_active: '&:active, &[data-active=true]', | ||
_focus: '&:focus', | ||
_visited: '&:visited', | ||
_even: '&:nth-of-type(even)', | ||
_odd: '&:nth-of-type(odd)', | ||
_disabled: '&:disabled, &:disabled:focus, &:disabled:hover, &[aria-disabled=true], &[aria-disabled=true]:focus, &[aria-disabled=true]:hover', | ||
_checked: '&[aria-checked=true]', | ||
_mixed: '&[aria-checked=mixed]', | ||
_selected: '&[aria-selected=true]', | ||
_invalid: '&[aria-invalid=true]', | ||
_pressed: '&[aria-pressed=true]', | ||
_readOnly: '&[aria-readonly=true], &[readonly]', | ||
_first: '&:first-of-type', | ||
_last: '&:last-of-type', | ||
_expanded: '&[aria-expanded=true]', | ||
_grabbed: '&[aria-grabbed=true]', | ||
_notFirst: '&:not(:first-of-type)', | ||
_notLast: '&:not(:last-of-type)', | ||
_groupHover: '[role=group]:hover &', | ||
_before: '&:before', | ||
_after: '&:after', | ||
_focusWithin: '&:focus-within', | ||
_placeholder: '&::placeholder' | ||
} | ||
|
||
/** | ||
* Filter undefined props and parse pseudo style props to generate css styles object | ||
* @param {Object} props - Props | ||
* @returns {Object} Parsed pseudo styles object | ||
*/ | ||
export function parsePseudoStyles (props) { | ||
const styles = {} | ||
const clean = cleanProps(props) | ||
const pseudoProps = filterPseudo(clean) | ||
const result = map(pseudoProps, (value, prop) => ({ prop, value })) | ||
result.forEach((pair) => { | ||
if (selectors[pair.prop]) { | ||
styles[selectors[pair.prop]] = tx(pair.value) | ||
} | ||
}) | ||
return styles | ||
} |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { default as baseProps } from './props' | ||
export { default as propsConfig } from './props.config' | ||
export { default as pseudoProps } from './pseudo' |
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,7 @@ | ||
const pseudoProps = { | ||
_hover: [Object, String, Array], | ||
_active: [Object, String, Array], | ||
_focus: [Object, String, Array] | ||
} | ||
|
||
export default pseudoProps |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export { default as Box } from '../../components/Box' | ||
export { default as PseudoBox } from '../../components/PseudoBox' |
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 |
---|---|---|
@@ -1,7 +1,4 @@ | ||
import Logger from './logger' | ||
import { provideTheme } from './provide-theme' | ||
|
||
export { | ||
Logger, | ||
provideTheme | ||
} | ||
export { default as Logger } from './logger' | ||
export { provideTheme } from './provide-theme' | ||
export { transformAlias as tx } from './transform' | ||
export { pickProperty as cleanProps, filterPseudo } from './object' |
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,19 @@ | ||
import pickBy from 'lodash-es/pickBy' | ||
import startsWith from 'lodash-es/startsWith' | ||
|
||
/** | ||
* @description Clears out all undefined properties from an object. | ||
* @param {Object} props | ||
* @returns {Object} Sanitized object with defined values. | ||
*/ | ||
export function pickProperty (props) { | ||
const pure = pickBy(props, (prop) => !!prop) | ||
return pure | ||
} | ||
|
||
export function filterPseudo (props) { | ||
const pseudos = pickBy(props, (_value, key) => { | ||
return startsWith(key, '_') | ||
}) | ||
return pseudos | ||
} |
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,46 @@ | ||
import { propsConfig as config } from '../config/props' | ||
|
||
/** | ||
* @description Transforms the custom prop alias to a format that styled-system CSS supports | ||
* @param {*} prop - Prop | ||
* @param {*} propValue - Prop value | ||
* @returns {Object} Style object with transformed alias. | ||
* @see chakra-ui PseudoBox tx_ method. | ||
*/ | ||
function normalizeAlias (prop, propValue) { | ||
const configKeys = Object.keys(config) | ||
let result = {} | ||
|
||
if (configKeys.includes(prop)) { | ||
const { properties, property } = config[prop] | ||
if (properties) { | ||
properties.forEach(_cssProp => (result[_cssProp] = propValue)) | ||
} | ||
if (property) { | ||
result[property] = propValue | ||
} | ||
if (config[prop] === true) { | ||
result[prop] = propValue | ||
} | ||
} else { | ||
result[prop] = propValue | ||
} | ||
return result | ||
} | ||
|
||
/** | ||
* @description Transforms the alias prop object to style-system supported syntax | ||
* @param {Object} props - Props object | ||
* @returns {Object} Normalized Props object | ||
*/ | ||
export const transformAlias = props => { | ||
let result = {} | ||
for (let prop in props) { | ||
if (typeof props[prop] === 'object') { | ||
result = { ...result, [prop]: transformAlias(props[prop]) } | ||
} else { | ||
result = { ...result, ...normalizeAlias(prop, props[prop]) } | ||
} | ||
} | ||
return result | ||
} |