Skip to content

Commit

Permalink
cx to cn
Browse files Browse the repository at this point in the history
Co-authored-by: Jiachi Liu <inbox@huozhi.im>
  • Loading branch information
devjiwonchoi and huozhi committed Feb 6, 2025
1 parent 0382a71 commit 56cf74a
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 58 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react'
import { noop as css } from '../../helpers/noop-template'
import { cn } from '../../helpers/merge-class-names'
import { cx } from '../../helpers/cx'

function useCopyLegacy(content: string) {
type CopyState =
Expand Down Expand Up @@ -185,7 +185,7 @@ export function CopyButton({
aria-disabled={isDisabled}
disabled={isDisabled}
data-nextjs-data-runtime-error-copy-button
className={cn(
className={cx(
props.className,
'nextjs-data-runtime-error-copy-button',
`nextjs-data-runtime-error-copy-button--${copyState.state}`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { DialogHeader } from '../../dialog/dialog-header'
import { noop as css } from '../../../helpers/noop-template'
import { cn } from '../../../helpers/merge-class-names'
import { cx } from '../../../helpers/cx'

type ErrorOverlayDialogHeaderProps = {
children?: React.ReactNode
Expand All @@ -13,7 +13,7 @@ export function ErrorOverlayDialogHeader({
}: ErrorOverlayDialogHeaderProps) {
return (
<DialogHeader
className={cn(
className={cx(
'nextjs-container-errors-header',
isTurbopack && 'nextjs-error-overlay-dialog-header-turbopack-background'
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useState, useCallback } from 'react'
import { ThumbsUp } from '../../../../icons/thumbs/thumbs-up'
import { ThumbsDown } from '../../../../icons/thumbs/thumbs-down'
import { noop as css } from '../../../../helpers/noop-template'
import { cn } from '../../../../helpers/merge-class-names'
import { cx } from '../../../../helpers/cx'

interface ErrorFeedbackProps {
errorCode: string
Expand Down Expand Up @@ -44,7 +44,7 @@ export function ErrorFeedback({ errorCode, className }: ErrorFeedbackProps) {

return (
<div
className={cn('error-feedback', className)}
className={cx('error-feedback', className)}
role="region"
aria-label="Error feedback"
>
Expand All @@ -58,15 +58,15 @@ export function ErrorFeedback({ errorCode, className }: ErrorFeedbackProps) {
<button
aria-label="Mark as helpful"
onClick={() => handleFeedback(true)}
className={cn('feedback-button', voted === true && 'voted')}
className={cx('feedback-button', voted === true && 'voted')}
type="button"
>
<ThumbsUp aria-hidden="true" />
</button>
<button
aria-label="Mark as not helpful"
onClick={() => handleFeedback(false)}
className={cn('feedback-button', voted === false && 'voted')}
className={cx('feedback-button', voted === false && 'voted')}
type="button"
>
<ThumbsDown
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as React from 'react'
import { cn } from '../../helpers/merge-class-names'
import { cx } from '../../helpers/cx'
export type ToastProps = React.HTMLProps<HTMLDivElement> & {
children?: React.ReactNode
onClick?: () => void
Expand All @@ -19,7 +19,7 @@ export const Toast: React.FC<ToastProps> = function Toast({
e.preventDefault()
return onClick?.()
}}
className={cn('nextjs-toast', className)}
className={cx('nextjs-toast', className)}
>
<div data-nextjs-toast-wrapper>{children}</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { VersionInfo } from '../../../../../../../server/dev/parse-version-info'
import { cn } from '../../helpers/merge-class-names'
import { cx } from '../../helpers/cx'
import { noop as css } from '../../helpers/noop-template'

export function VersionStalenessInfo({
Expand All @@ -17,13 +17,13 @@ export function VersionStalenessInfo({

return (
<span
className={cn(
className={cx(
'nextjs-container-build-error-version-status',
'dialog-exclude-closing-from-outside-click',
isTurbopack && 'turbopack-border'
)}
>
<Eclipse className={cn('version-staleness-indicator', indicatorClass)} />
<Eclipse className={cx('version-staleness-indicator', indicatorClass)} />
<span data-nextjs-version-checker title={title}>
{text}
</span>{' '}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { cx } from './cx'

describe('cx', () => {
it('should return an empty string for no arguments', () => {
expect(cx()).toBe('')
})

it('should join multiple string arguments with spaces', () => {
expect(cx('foo', 'bar', 'baz')).toBe('foo bar baz')
})

it('should filter out falsy values', () => {
expect(cx('foo', null, 'bar', undefined, 'baz', false)).toBe('foo bar baz')
})

it('should handle single string argument', () => {
expect(cx('foo')).toBe('foo')
})

it('should not add extra spaces when falsy values are between strings', () => {
expect(cx('foo', null, 'bar')).toBe('foo bar')
})

it('should handle all falsy values', () => {
expect(cx(null, undefined, false)).toBe('')
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Merge multiple args to a single string with spaces. Useful for merging class names.
* @example
* cx('foo', 'bar') // 'foo bar'
* cx('foo', null, 'bar', undefined, 'baz', false) // 'foo bar baz'
*/
export function cx(...args: (string | undefined | null | false)[]): string {
return args.filter(Boolean).join(' ')
}

This file was deleted.

This file was deleted.

0 comments on commit 56cf74a

Please sign in to comment.