lukeed/clsxPublic

A tiny (239B) utility for constructing `className` strings conditionally.

fix: return undefined instead of empty string to prevent empty class attribute #114

Closedli-jia-nanclosed 1 commit intomasterfromrefs/pull/114/head
  1. LN

    ## Summary Changes the `clsx` function to return `undefined` instead of an empty string when no valid class names are provided.

    ## Problem When `clsx` returns an empty string (`""`), JSX still renders the `class` attribute as `class=""` in the DOM, which is unnecessary and can cause issues in some cases.

    ## Solution Modified the return statement to use `|| undefined`, so when the resulting string is empty, the function returns `undefined` instead. This prevents JSX from rendering the `class` attribute at all.

    ## Examples

    **Before:**

    ```tsx <div className={clsx("")} /> // renders: <div class="" /> <div className={clsx(null, false)} /> // renders: <div class="" /> ``` **After:**

    ```tsx <div className={clsx("")} /> // renders: <div /> <div className={clsx(null, false)} /> // renders: <div /> ```

    ## Impact No breaking changes for existing functionality Cleaner DOM output when no classes are applied Better JSX behavior alignment with common expectations

  2. li-jia-nanrequested review from Copilot10 months ago on GitHub

  3. li-jia-nancopilot-work-started10 months ago on GitHub

  4. li-jia-nanreferenced this in fix: improvement style order #5659710 months ago on GitHub

  5. Hidden as resolved.
  6. Hidden as resolved.
  7. ## Pull request overview

    This pull request changes the `clsx` function to return `undefined` instead of an empty string when no class names are provided.

    **Changes:** - Modified the return statement in `clsx` function to return `undefined` when `str` is empty

    ---

    馃挕 <a href="/lukeed/clsx/new/master/.github/instructions?filename=*.instructions.md" class="Link--inTextBlock" target="_blank" rel="noopener noreferrer">Add Copilot custom instructions</a> for smarter, more guided reviews. <a href="https://docs.github.com/en/copilot/customizing-copilot/adding-repository-custom-instructions-for-github-copilot" class="Link--inTextBlock" target="_blank" rel="noopener noreferrer">Learn how to get started</a>.

  8. li-jia-nanrenamed Return undefined if str is empty to fix: return undefined instead of empty string to prevent empty class attribute10 months ago on GitHub

  9. lukeedclosed10 months ago on GitHub