Files
twenty/packages/twenty-ui/src/data-display/Chip/Chip.tsx
T
Raphaël Bosi 8034c7725f Reorganize twenty-ui into best-practice component domains and per-component folders (#21745)
Reorganizes `twenty-ui`'s component organization to follow how the best
UI libraries (MUI, Mantine, Base UI, Polaris) structure their source,
now that the package has stabilized.

**Taxonomy** — dissolves the meaningless `components/` junk-drawer and
the 107-file `display/` mega-category. New domains/subpaths:
`data-display`, `typography`, `icon`, `surfaces`; `feedback` and
`layout` absorb the rest (banners/callout/info + placeholders →
feedback; modal/card → surfaces; motion + separators → layout).

**Per-component layout** — every component is now
`<domain>/<ComponentName>/<ComponentName>.tsx` with colocated
styles/stories/types, `internal/` for private helpers and `parts/` for
re-exported compound sub-parts. The redundant inner `/components/` is
gone. `icon` and `json-visualizer` are kept as cohesive subsystems.

**Also:** adds a tree-shakeable root barrel (`import { Button } from
'twenty-ui'`), the generator now owns `individual-entry.ts`, and a real
barrel-leak bug is fixed (private `internals/` parts were leaking into
the public API).

Consumer imports (~1.2k files) and the `twenty-sdk` UI aggregator were
updated by codemod. The change is **export-neutral** except 16
intentionally-removed private internals symbols (all verified
unconsumed). Gates green: typecheck, lint, build, size-limit, storybook.


<!-- This is an auto-generated description by cubic. -->
<a
href="https://cubic.dev/pr/twentyhq/twenty/pull/21745?utm_source=github"
target="_blank" rel="noopener noreferrer"
data-no-image-dialog="true"><picture><source
media="(prefers-color-scheme: dark)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source
media="(prefers-color-scheme: light)"
srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img
alt="Review in cubic"
src="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"></picture></a>
<!-- End of auto-generated description by cubic. -->
2026-06-18 10:31:29 +02:00

150 lines
3.8 KiB
TypeScript

import { type ReactNode } from 'react';
import { isNonEmptyString } from '@sniptt/guards';
import { OverflowingTextWithTooltip } from '@ui/surfaces/OverflowingTextWithTooltip/OverflowingTextWithTooltip';
import { clsx } from 'clsx';
import { isDefined } from '@ui/utilities/utils/isDefined';
import styles from './Chip.module.scss';
export enum ChipSize {
Large = 'large',
Small = 'small',
}
export enum ChipAccent {
TextPrimary = 'text-primary',
TextSecondary = 'text-secondary',
}
export enum ChipVariant {
Highlighted = 'highlighted',
Regular = 'regular',
Transparent = 'transparent',
Rounded = 'rounded',
Static = 'static',
}
export type ChipProps = {
size?: ChipSize;
disabled?: boolean;
clickable?: boolean;
label: string;
tooltipLabel?: string;
alwaysShowTooltip?: boolean;
isLabelHidden?: boolean;
isBold?: boolean;
maxWidth?: number;
variant?: ChipVariant;
accent?: ChipAccent;
leftComponent?: ReactNode | null;
rightComponent?: (() => ReactNode) | ReactNode | null;
rightComponentDivider?: boolean;
className?: string;
forceEmptyText?: boolean;
emptyLabel?: string;
};
const renderRightComponent = (
rightComponent: (() => ReactNode) | ReactNode | null,
rightComponentDivider?: boolean,
) => {
if (!rightComponent) {
return null;
}
const rendered =
typeof rightComponent === 'function' ? rightComponent() : rightComponent;
if (rightComponentDivider === true) {
return (
<>
<div className={styles.rightComponentDivider} />
{rendered}
</>
);
}
return rendered;
};
export const Chip = ({
size = ChipSize.Small,
label,
tooltipLabel,
alwaysShowTooltip = false,
isLabelHidden = false,
isBold = false,
disabled = false,
clickable = true,
variant = ChipVariant.Regular,
leftComponent = null,
rightComponent = null,
rightComponentDivider = false,
accent = ChipAccent.TextPrimary,
className,
maxWidth,
forceEmptyText = false,
emptyLabel = 'Untitled',
}: ChipProps) => {
// Cursor precedence mirrors the Linaria ternary:
// transparent > clickable > disabled > inherit.
const cursorClass =
variant === ChipVariant.Transparent
? undefined
: clickable
? styles.cursorPointer
: disabled
? styles.cursorNotAllowed
: undefined;
const backgroundClass =
variant === ChipVariant.Highlighted
? styles.backgroundHighlighted
: variant === ChipVariant.Static
? styles.backgroundStatic
: variant === ChipVariant.Regular && !disabled && clickable
? styles.interactiveRegular
: undefined;
return (
<div
data-testid="chip"
className={clsx(
styles.chip,
size === ChipSize.Large ? styles.sizeLarge : styles.sizeSmall,
accent === ChipAccent.TextPrimary
? styles.accentTextPrimary
: styles.accentTextSecondary,
disabled && styles.disabled,
(isBold || accent === ChipAccent.TextSecondary) && styles.fontMedium,
cursorClass,
backgroundClass,
maxWidth ? styles.hasMaxWidth : undefined,
variant === ChipVariant.Transparent && styles.paddingLeftNone,
className,
)}
style={
maxWidth
? ({ '--chip-max-width': `${maxWidth}px` } as React.CSSProperties)
: undefined
}
>
{leftComponent}
{!isLabelHidden && isDefined(label) && isNonEmptyString(label) ? (
<OverflowingTextWithTooltip
size={size}
text={label}
tooltipContent={tooltipLabel}
alwaysShowTooltip={alwaysShowTooltip}
/>
) : !forceEmptyText && !isLabelHidden ? (
<div className={styles.emptyLabel}>{emptyLabel}</div>
) : (
''
)}
{renderRightComponent(rightComponent, rightComponentDivider)}
</div>
);
};