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

Cn utility refactor #2915

Merged
merged 3 commits into from
Apr 29, 2024
Merged
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
7 changes: 7 additions & 0 deletions .changeset/few-oranges-jam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@nextui-org/system": patch
"@nextui-org/system-rsc": patch
"@nextui-org/theme": patch
---

The `cn` utility was moved the `theme` package and updated to support NextUI custom classes.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct the grammatical error in the description.

- The `cn` utility was moved the `theme` package and updated to support NextUI custom classes.
+ The `cn` utility was moved to the `theme` package and updated to support NextUI custom classes.

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
The `cn` utility was moved the `theme` package and updated to support NextUI custom classes.
The `cn` utility was moved to the `theme` package and updated to support NextUI custom classes.

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import {I18nProvider, useLocale} from "@react-aria/i18n";
import {Button, ButtonGroup} from "@nextui-org/button";
import {Radio, RadioGroup} from "@nextui-org/radio";
import {cn} from "@nextui-org/system";
import {cn} from "@nextui-org/theme";

import {Calendar, CalendarProps, DateValue} from "../src";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
import {I18nProvider, useLocale} from "@react-aria/i18n";
import {Button, ButtonGroup} from "@nextui-org/button";
import {Radio, RadioGroup} from "@nextui-org/radio";
import {cn} from "@nextui-org/system";
import {cn} from "@nextui-org/theme";

import {RangeCalendar, RangeCalendarProps} from "../src";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
import {I18nProvider, useDateFormatter, useLocale} from "@react-aria/i18n";
import {Button, ButtonGroup} from "@nextui-org/button";
import {Radio, RadioGroup} from "@nextui-org/radio";
import {cn} from "@nextui-org/system";
import {cn} from "@nextui-org/theme";

import {DatePicker, DatePickerProps} from "../src";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {DateValue} from "@react-types/datepicker";
import {I18nProvider, useDateFormatter, useLocale} from "@react-aria/i18n";
import {Button, ButtonGroup} from "@nextui-org/button";
import {Radio, RadioGroup} from "@nextui-org/radio";
import {cn} from "@nextui-org/system";
import {cn} from "@nextui-org/theme";

import {DateRangePicker, DateRangePickerProps} from "../src";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from "react";
import {Meta} from "@storybook/react";
import {button, pagination} from "@nextui-org/theme";
import {cn} from "@nextui-org/system";
import {cn} from "@nextui-org/theme";
import {ChevronIcon} from "@nextui-org/shared-icons";

import {Pagination, PaginationItemRenderProps, PaginationItemType, usePagination} from "../src";
Expand Down
2 changes: 1 addition & 1 deletion packages/components/slider/stories/slider.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {Meta} from "@storybook/react";
import {slider} from "@nextui-org/theme";
import {InfoIcon, VolumeHighBoldIcon, VolumeLowBoldIcon} from "@nextui-org/shared-icons";
import {Tooltip} from "@nextui-org/tooltip";
import {cn} from "@nextui-org/system";
import {cn} from "@nextui-org/theme";

import {Slider, SliderProps, SliderValue} from "../src";

Expand Down
3 changes: 1 addition & 2 deletions packages/core/system-rsc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@
},
"peerDependencies": {
"react": ">=18",
"@nextui-org/theme": ">=2.1.0",
"tailwind-variants": ">=0.1.13"
"@nextui-org/theme": ">=2.1.0"
},
"devDependencies": {
"react": "^18.0.0",
Expand Down
7 changes: 4 additions & 3 deletions packages/core/system-rsc/src/extend-variants.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import * as React from "react";
import {tv} from "@nextui-org/theme";
import clsx from "clsx";

import {cn, mapPropsVariants} from "./utils";
import {mapPropsVariants} from "./utils";

function getSlots(variants) {
return variants
Expand Down Expand Up @@ -65,7 +66,7 @@ function getClassNamesWithProps({

// if no slots, the result is a string
if (!hasSlots) {
newProps.className = cn(result, props.className);
newProps.className = clsx(result, props.className);
}
// if has slots, the result is an object with keys as slots functions
else {
Expand All @@ -78,7 +79,7 @@ function getClassNamesWithProps({
});

Object.entries(props.classNames ?? {}).forEach(([key, value]) => {
classNames[key] = cn(classNames[key], value);
classNames[key] = clsx(classNames[key], value);
});
}

Expand Down
1 change: 0 additions & 1 deletion packages/core/system-rsc/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export type {
} from "./types";

export {
cn,
forwardRef,
toIterator,
mapPropsVariants,
Expand Down
6 changes: 0 additions & 6 deletions packages/core/system-rsc/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type {As, RightJoinProps, PropsOf, InternalForwardRefRenderFunction} from "./types";

import * as React from "react";
import clsx from "clsx";
import {forwardRef as baseForwardRef} from "react";

export function forwardRef<
Expand Down Expand Up @@ -96,11 +95,6 @@ export const mapPropsVariantsWithCommon = <
return [props, variants] as const;
};

/**
* Classnames utility
*/
export const cn = clsx;

/**
* Checks if a component is a NextUI component.
* @param component - The component to check.
Expand Down
5 changes: 3 additions & 2 deletions packages/core/system-rsc/test-utils/slots-component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import React, {useMemo} from "react";
import {SlotsToClasses, tv, type VariantProps} from "@nextui-org/theme";
import {filterDOMProps, ReactRef, useDOMRef} from "@nextui-org/react-utils";
import {objectToDeps} from "@nextui-org/shared-utils";
import clsx from "clsx";

import {cn, forwardRef, mapPropsVariants} from "../src/utils";
import {forwardRef, mapPropsVariants} from "../src/utils";

const card = tv({
slots: {
Expand Down Expand Up @@ -171,7 +172,7 @@ export const Card = forwardRef<"div", CardProps>((originalProps, ref) => {

const styles = useMemo(() => card({...variantProps}), [objectToDeps(variantProps)]);

const baseStyles = cn(classNames?.base, className);
const baseStyles = clsx(classNames?.base, className);

const domRef = useDOMRef(ref);

Expand Down
1 change: 0 additions & 1 deletion packages/core/system/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export type {
} from "@nextui-org/system-rsc";

export {
cn,
forwardRef,
toIterator,
mapPropsVariants,
Expand Down
4 changes: 3 additions & 1 deletion packages/core/theme/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@
"lodash.kebabcase": "^4.1.1",
"lodash.mapkeys": "^4.6.0",
"lodash.omit": "^4.5.0",
"tailwind-variants": "^0.1.20"
"clsx": "^1.2.1",
"tailwind-variants": "^0.1.20",
"tailwind-merge": "^1.14.0"
},
"peerDependencies": {
"tailwindcss": ">=3.4.0"
Expand Down
17 changes: 17 additions & 0 deletions packages/core/theme/src/utils/cn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import type {ClassValue} from "clsx";

import clsx from "clsx";
import {extendTailwindMerge} from "tailwind-merge";

import {twMergeConfig} from "./tw-merge-config";

/**
* We need to extend the tailwind merge to include NextUI's custom classes.
*
* So we can use classes like `text-small` or `text-default-500` and override them.
*/
const twMerge = extendTailwindMerge(twMergeConfig);

export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}
17 changes: 14 additions & 3 deletions packages/core/theme/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
export * from "./classes";
export * from "./types";
export * from "./variants";
export {
baseStyles,
ringClasses,
focusVisibleClasses,
dataFocusVisibleClasses,
groupDataFocusVisibleClasses,
translateCenterClasses,
absoluteFullClasses,
collapseAdjacentVariantBorders,
} from "./classes";
export type {SlotsToClasses} from "./types";
export {colorVariants} from "./variants";
export {COMMON_UNITS, twMergeConfig} from "./tw-merge-config";
export {cn} from "./cn";
11 changes: 3 additions & 8 deletions packages/core/theme/src/utils/tv.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {tv as tvBase, TV} from "tailwind-variants";

const COMMON_UNITS = ["small", "medium", "large"];
import {twMergeConfig} from "./tw-merge-config";

export const tv: TV = (options, config) =>
tvBase(options, {
Expand All @@ -10,16 +10,11 @@ export const tv: TV = (options, config) =>
...config?.twMergeConfig,
theme: {
...config?.twMergeConfig?.theme,
opacity: ["disabled"],
spacing: ["divider"],
borderWidth: COMMON_UNITS,
borderRadius: COMMON_UNITS,
...twMergeConfig.theme,
},
classGroups: {
...config?.twMergeConfig?.classGroups,
shadow: [{shadow: COMMON_UNITS}],
"font-size": [{text: ["tiny", ...COMMON_UNITS]}],
"bg-image": ["bg-stripe-gradient"],
...twMergeConfig.classGroups,
},
},
});
16 changes: 16 additions & 0 deletions packages/core/theme/src/utils/tw-merge-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type {Config} from "tailwind-merge";
export const COMMON_UNITS = ["small", "medium", "large"];

export const twMergeConfig: Partial<Config> = {
theme: {
opacity: ["disabled"],
spacing: ["divider"],
borderWidth: COMMON_UNITS,
borderRadius: COMMON_UNITS,
},
classGroups: {
shadow: [{shadow: COMMON_UNITS}],
"font-size": [{text: ["tiny", ...COMMON_UNITS]}],
"bg-image": ["bg-stripe-gradient"],
},
};
6 changes: 6 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading