Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for #650: Generate friendly class names #916

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 baseClass = componentName && componentName.length > 0 ? `${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-${baseClass}`

/** @type {VariantTuple[]} */
const singularVariants = []
Expand Down
17 changes: 17 additions & 0 deletions packages/core/tests/component-friendly-class-names.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { createStitches } from '../src/index.js'

describe('Generate human-legible class names', () => {
test('Class name includes "wrapper"', () => {
const { css } = createStitches()

const wrapper = css({}, "wrapper")
expect(wrapper.className).toBe('c-wrapper-PJLV')
})

test('Class name includes "heading"', () => {
const { css } = createStitches()

const heading = css({}, "heading")
expect(heading.className).toBe('c-heading-PJLV')
})
})
2 changes: 1 addition & 1 deletion packages/core/types/stitches.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ type ThemeTokens<Values, Prefix> = {
[Scale in keyof Values]: {
[Token in keyof Values[Scale]]: ThemeUtil.Token<
Extract<Token, number | string>,
Values[Scale][Token],
Values[Scale][Token] & (string | number),
Extract<Scale, string | void>,
Extract<Prefix, string | void>
>
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
19 changes: 19 additions & 0 deletions packages/react/tests/component-friendly-class-names.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { createStitches } from '../src/index.js'

describe('Generate human-legible class names', () => {
test('Renders a DIV with "Wrapper" in its class name', () => {
const { styled } = createStitches()

const Wrapper = styled('div', {}, "Wrapper")
const expression = Wrapper.render()
expect(expression.props.className).toBe("c-Wrapper-PJLV")
})

test('Renders an H1 with "Heading" in its class name', () => {
const { styled } = createStitches()

const Heading = styled('h1', {}, "Heading")
const expression = Heading.render()
expect(expression.props.className).toBe("c-Heading-PJLV")
})
})
2 changes: 1 addition & 1 deletion packages/react/types/stitches.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ type ThemeTokens<Values, Prefix> = {
[Scale in keyof Values]: {
[Token in keyof Values[Scale]]: ThemeUtil.Token<
Extract<Token, number | string>,
Values[Scale][Token],
Values[Scale][Token] & (string | number),
Extract<Scale, string | void>,
Extract<Prefix, string | void>
>
Expand Down
2 changes: 1 addition & 1 deletion packages/react/types/styled-component.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export interface StyledComponent<
? React.ComponentPropsWithRef<Type>
: {},
TransformProps<Props, Media> & {
as?: never,
as?: Type extends string | React.ComponentType<any> ? IntrinsicElementsKeys | React.ComponentType<any> : never,
css?: CSS
}
>
Expand Down