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
  • Loading branch information
LucasUnplugged committed Dec 21, 2021
1 parent 3b323e5 commit 0a1cdd9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
11 changes: 8 additions & 3 deletions packages/core/src/features/css.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ 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 componentName = args[last] && typeof args[last] === 'string' ? args[last] : null

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

// otherwise, add a new composer to this component
else {
internals.composers.add(createComposer(arg, config))
internals.composers.add(createComposer({componentName, ...arg}, config))
}
}

Expand All @@ -66,9 +69,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
6 changes: 5 additions & 1 deletion packages/react/src/features/styled.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ 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 displayName = args[last] && typeof args[last] === 'string'
? args[last]
: DefaultType.displayName || DefaultType.name || DefaultType

const styledComponent = React.forwardRef((props, ref) => {
const Type = props && props.as || DefaultType
Expand All @@ -38,7 +42,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 0a1cdd9

Please sign in to comment.