Skip to content

Commit

Permalink
fix(650): Added ability to define friendly class names.
Browse files Browse the repository at this point in the history
Squashed commits:
[a3e0552]
[0a1cdd9]
  • Loading branch information
LucasUnplugged committed Jan 3, 2022
1 parent 73646c6 commit fba8bb5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
14 changes: 10 additions & 4 deletions packages/core/src/features/css.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ const createCssFunctionMap = createMemo()
/** Returns a function that applies component styles. */
export const createCssFunction = (/** @type {Config} */ config, /** @type {SheetGroup} */ sheet) =>
createCssFunctionMap(config, () => (...args) => {
const last = args.length - 1
const hasCustomName = last > 0 && args[last] && typeof args[last] === 'string'
const componentName = hasCustomName ? args[last].replace(/\./g, '_').replace(/\W/g, '') : null

/** @type {Internals} */
let internals = {
type: null,
Expand All @@ -53,8 +57,8 @@ export const createCssFunction = (/** @type {Config} */ config, /** @type {Sheet
}

// otherwise, add a new composer to this component
else {
internals.composers.add(createComposer(arg, config))
else if (typeof arg !== 'string') {
internals.composers.add(createComposer({componentName, ...arg}, config))
}
}

Expand All @@ -66,9 +70,11 @@ export const createCssFunction = (/** @type {Config} */ config, /** @type {Sheet
})

/** Creates a composer from a configuration object. */
const createComposer = (/** @type {InitComposer} */ { variants: initSingularVariants, compoundVariants: initCompoundVariants, defaultVariants: initDefaultVariants, ...style }, /** @type {Config} */ config) => {
const createComposer = (/** @type {InitComposer} */ { variants: initSingularVariants, compoundVariants: initCompoundVariants, defaultVariants: initDefaultVariants, componentName, ...style }, /** @type {Config} */ config) => {
/** @type {string} */
const hash = componentName ? `${componentName}-${toHash(style)}` : toHash(style)
/** @type {string} Composer Unique Identifier. @see `{CONFIG_PREFIX}-?c-{STYLE_HASH}` */
const className = `${toTailDashed(config.prefix)}c-${toHash(style)}`
const className = `${toTailDashed(config.prefix)}c-${hash}`

/** @type {VariantTuple[]} */
const singularVariants = []
Expand Down
7 changes: 6 additions & 1 deletion packages/react/src/features/styled.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ export const createStyledFunction = ({ /** @type {Config} */ config, /** @type {
const styled = (...args) => {
const cssComponent = css(...args)
const DefaultType = cssComponent[internal].type
const last = args.length - 1
const hasCustomName = last > 0 && args[last] && typeof args[last] === 'string'
const displayName = hasCustomName
? args[last].replace(/\./g, '_').replace(/\W/g, '')
: DefaultType.displayName || DefaultType.name || DefaultType

const styledComponent = React.forwardRef((props, ref) => {
const Type = props && props.as || DefaultType
Expand All @@ -38,7 +43,7 @@ export const createStyledFunction = ({ /** @type {Config} */ config, /** @type {
const toString = () => cssComponent.selector

styledComponent.className = cssComponent.className
styledComponent.displayName = `Styled.${DefaultType.displayName || DefaultType.name || DefaultType}`
styledComponent.displayName = `Styled.${displayName}`
styledComponent.selector = cssComponent.selector
styledComponent.toString = toString
styledComponent[internal] = cssComponent[internal]
Expand Down

0 comments on commit fba8bb5

Please sign in to comment.