PHP 8.2.30
Preview: class-names.tsx Size: 4.50 KB
/proc/thread-self/root/home/byroehnu/.trash/node_modules11/@emotion/react/src/class-names.tsx

import * as React from 'react'
import {
  EmotionCache,
  getRegisteredStyles,
  insertStyles,
  registerStyles,
  SerializedStyles
} from '@emotion/utils'
import { CSSInterpolation, serializeStyles } from '@emotion/serialize'
import isDevelopment from '#is-development'
import { withEmotionCache } from './context'
import { Theme, ThemeContext } from './theming'
import { useInsertionEffectAlwaysWithSyncFallback } from '@emotion/use-insertion-effect-with-fallbacks'
import isBrowser from '#is-browser'

export interface ArrayClassNamesArg extends Array<ClassNamesArg> {}

export type ClassNamesArg =
  | undefined
  | null
  | string
  | boolean
  | { [className: string]: boolean | null | undefined }
  | ArrayClassNamesArg

let classnames = (args: ArrayClassNamesArg): string => {
  let len = args.length
  let i = 0
  let cls = ''
  for (; i < len; i++) {
    let arg = args[i]
    if (arg == null) continue

    let toAdd
    switch (typeof arg) {
      case 'boolean':
        break
      case 'object': {
        if (Array.isArray(arg)) {
          toAdd = classnames(arg)
        } else {
          if (
            isDevelopment &&
            arg.styles !== undefined &&
            arg.name !== undefined
          ) {
            console.error(
              'You have passed styles created with `css` from `@emotion/react` package to the `cx`.\n' +
                '`cx` is meant to compose class names (strings) so you should convert those styles to a class name by passing them to the `css` received from <ClassNames/> component.'
            )
          }
          toAdd = ''
          for (const k in arg) {
            if (arg[k] && k) {
              toAdd && (toAdd += ' ')
              toAdd += k
            }
          }
        }
        break
      }
      default: {
        toAdd = arg
      }
    }
    if (toAdd) {
      cls && (cls += ' ')
      cls += toAdd
    }
  }
  return cls
}
function merge(
  registered: EmotionCache['registered'],
  css: ClassNamesContent['css'],
  className: string
) {
  const registeredStyles: string[] = []

  const rawClassName = getRegisteredStyles(
    registered,
    registeredStyles,
    className
  )

  if (registeredStyles.length < 2) {
    return className
  }
  return rawClassName + css(registeredStyles)
}

const Insertion = ({
  cache,
  serializedArr
}: {
  cache: EmotionCache
  serializedArr: SerializedStyles[]
}) => {
  let rules = useInsertionEffectAlwaysWithSyncFallback(() => {
    let rules = ''
    for (let i = 0; i < serializedArr.length; i++) {
      let res = insertStyles(cache, serializedArr[i], false)
      if (!isBrowser && res !== undefined) {
        rules += res
      }
    }
    if (!isBrowser) {
      return rules
    }
  })

  if (!isBrowser && rules!.length !== 0) {
    return (
      <style
        {...{
          [`data-emotion`]: `${cache.key} ${serializedArr
            .map(serialized => serialized.name)
            .join(' ')}`,
          dangerouslySetInnerHTML: { __html: rules! },
          nonce: cache.sheet.nonce
        }}
      />
    )
  }
  return null
}

export interface ClassNamesContent {
  css(template: TemplateStringsArray, ...args: Array<CSSInterpolation>): string
  css(...args: Array<CSSInterpolation>): string
  cx(...args: Array<ClassNamesArg>): string
  theme: Theme
}

export interface ClassNamesProps {
  children(content: ClassNamesContent): React.ReactNode
}

export const ClassNames = /* #__PURE__ */ withEmotionCache<ClassNamesProps>(
  (props, cache) => {
    let hasRendered = false
    let serializedArr: SerializedStyles[] = []

    let css: ClassNamesContent['css'] = (...args) => {
      if (hasRendered && isDevelopment) {
        throw new Error('css can only be used during render')
      }

      let serialized = serializeStyles(args, cache.registered)
      serializedArr.push(serialized)
      // registration has to happen here as the result of this might get consumed by `cx`
      registerStyles(cache, serialized, false)
      return `${cache.key}-${serialized.name}`
    }
    let cx = (...args: Array<ClassNamesArg>) => {
      if (hasRendered && isDevelopment) {
        throw new Error('cx can only be used during render')
      }
      return merge(cache.registered, css, classnames(args))
    }
    let content = {
      css,
      cx,
      theme: React.useContext(ThemeContext)
    }
    let ele = props.children(content)
    hasRendered = true

    return (
      <>
        <Insertion cache={cache} serializedArr={serializedArr} />
        {ele}
      </>
    )
  }
)

if (isDevelopment) {
  ClassNames.displayName = 'EmotionClassNames'
}

Directory Contents

Dirs: 1 × Files: 16

Name Size Perms Modified Actions
- drwxr-xr-x 2026-02-28 00:30:22
Edit Download
4.50 KB lrw-r--r-- 2026-02-28 00:30:24
Edit Download
2.49 KB lrw-r--r-- 2026-02-28 00:30:26
Edit Download
423 B lrw-r--r-- 2026-02-28 00:30:08
Edit Download
4.99 KB lrw-r--r-- 2026-02-28 00:30:26
Edit Download
1.61 KB lrw-r--r-- 2026-02-28 00:30:12
Edit Download
4.49 KB lrw-r--r-- 2026-02-28 00:30:26
Edit Download
2.08 KB lrw-r--r-- 2026-02-28 00:30:14
Edit Download
826 B lrw-r--r-- 2026-02-28 00:30:14
Edit Download
4.23 KB lrw-r--r-- 2026-02-28 00:30:16
Edit Download
924 B lrw-r--r-- 2026-02-28 00:30:16
Edit Download
1.55 KB lrw-r--r-- 2026-02-28 00:30:16
Edit Download
654 B lrw-r--r-- 2026-02-28 00:30:20
Edit Download
2.85 KB lrw-r--r-- 2026-02-28 00:30:28
Edit Download
544 B lrw-r--r-- 2026-02-28 00:30:24
Edit Download
40 B lrw-r--r-- 2026-02-28 00:30:24
Edit Download
650 B lrw-r--r-- 2026-02-28 00:30:08
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).