Bin
2025-12-16 9e0b2ba2c317b1a86212f24cbae3195ad1f3dbfa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import type React from "react";
import { forwardRef, useState, useEffect, useRef } from "react";
import { cnm } from "../../utils/utils";
import styles from "./typography.module.scss";
 
// Size constants
const SIZES = {
  LARGE: "large",
  MEDIUM: "medium",
  SMALL: "small",
  SMALLER: "smaller",
  SMALLEST: "smallest",
} as const;
 
type Sizes = {
  display: typeof SIZES.LARGE | typeof SIZES.MEDIUM | typeof SIZES.SMALL;
  headline: typeof SIZES.LARGE | typeof SIZES.MEDIUM | typeof SIZES.SMALL;
  title: typeof SIZES.LARGE | typeof SIZES.MEDIUM | typeof SIZES.SMALL;
  label: typeof SIZES.MEDIUM | typeof SIZES.SMALL | typeof SIZES.SMALLER | typeof SIZES.SMALLEST;
  body: typeof SIZES.MEDIUM | typeof SIZES.SMALL | typeof SIZES.SMALLER | typeof SIZES.SMALLEST;
};
 
type Variant = keyof Sizes;
 
const config = {
  display: {
    tag: { [SIZES.LARGE]: "h1", [SIZES.MEDIUM]: "h1", [SIZES.SMALL]: "h1" },
  },
  headline: {
    tag: { [SIZES.LARGE]: "h2", [SIZES.MEDIUM]: "h2", [SIZES.SMALL]: "h2" },
  },
  title: {
    tag: { [SIZES.LARGE]: "h3", [SIZES.MEDIUM]: "h4", [SIZES.SMALL]: "h5" },
  },
  label: {
    tag: { [SIZES.MEDIUM]: "p", [SIZES.SMALL]: "p", [SIZES.SMALLER]: "p", [SIZES.SMALLEST]: "p" },
  },
  body: {
    tag: { [SIZES.MEDIUM]: "p", [SIZES.SMALL]: "p", [SIZES.SMALLER]: "p", [SIZES.SMALLEST]: "p" },
  },
} as const satisfies {
  [V in Variant]: {
    tag: Record<Sizes[V], keyof JSX.IntrinsicElements>;
  };
};
 
type TypographyProps<V extends Variant = Variant> = {
  variant?: V;
  size?: Sizes[V];
  as?: keyof JSX.IntrinsicElements;
  className?: string;
  fontStyle?: "normal" | "italic";
  style?: React.CSSProperties;
  children?: React.ReactNode;
  truncateLines?: number;
  expandable?: boolean;
  expandLabel?: string;
  collapseLabel?: string;
  expandToggleClassName?: string;
} & Omit<React.HTMLAttributes<HTMLElement>, "style" | "className" | "children">;
 
const DEFAULT_TAG = "p";
const DEFAULT_CLASS = "typography-body-medium";
 
const Typography = forwardRef<HTMLElement, TypographyProps>(
  (
    {
      variant = "body",
      size = SIZES.MEDIUM,
      as,
      className,
      children,
      fontStyle = "normal",
      style,
      truncateLines,
      expandable = true,
      expandLabel = "Show more",
      collapseLabel = "Show less",
      expandToggleClassName,
      ...rest
    },
    ref,
  ) => {
    const variantConfig = config[variant];
    const tagMap = variantConfig?.tag;
    const tag = tagMap && size in tagMap ? tagMap[size as keyof typeof tagMap] : DEFAULT_TAG;
    const isValid = variant in config && tagMap && size in tagMap;
    const baseClass = isValid ? `typography-${variant}-${size}` : DEFAULT_CLASS;
    const Tag = (as || tag) as React.ElementType;
 
    const hasTruncation = truncateLines !== undefined && truncateLines > 0;
 
    // Only set up truncation logic if truncateLines is provided
    const [isExpanded, setIsExpanded] = useState(false);
    const [isClamped, setIsClamped] = useState(false);
    const contentRef = useRef<HTMLElement | null>(null);
 
    // Use internal ref for truncation, forwarded ref otherwise
    const elementRef = hasTruncation ? contentRef : (ref as React.Ref<HTMLElement>);
 
    // Check if content needs clamping using ResizeObserver
    useEffect(() => {
      if (!hasTruncation) return;
 
      const el = contentRef.current;
      if (!el) return;
 
      const compute = () => {
        const wasExpanded = isExpanded;
        // Temporarily collapse to check if clamping is needed
        if (wasExpanded) el.dataset.tmpCollapse = "1";
        const needsClamp = el.scrollHeight > el.clientHeight + 1;
        setIsClamped(needsClamp);
        if (wasExpanded) el.dataset.tmpCollapse = "";
      };
 
      compute();
      const ro = new ResizeObserver(() => compute());
      ro.observe(el);
      const onResize = () => compute();
      window.addEventListener("resize", onResize);
 
      return () => {
        ro.disconnect();
        window.removeEventListener("resize", onResize);
      };
    }, [hasTruncation, children, isExpanded]);
 
    // Apply inline styles for line-clamping when needed
    const clampStyles =
      hasTruncation && !isExpanded
        ? {
            display: "-webkit-box",
            WebkitLineClamp: truncateLines,
            WebkitBoxOrient: "vertical" as const,
            overflow: "hidden",
            ...style,
          }
        : style;
 
    return (
      <>
        <Tag
          ref={elementRef}
          className={cnm(styles[baseClass], fontStyle === "italic" && "italic", className)}
          style={clampStyles}
          {...(hasTruncation && {
            "data-tmp-collapse": !isExpanded ? undefined : "",
            "aria-expanded": isExpanded,
          })}
          {...rest}
        >
          {children}
        </Tag>
        {hasTruncation && expandable && (isClamped || isExpanded) && (
          <button
            type="button"
            onClick={() => setIsExpanded((v) => !v)}
            className={cnm(
              styles[baseClass],
              "text-primary-content hover:text-primary-content-hover block mt-1",
              expandToggleClassName,
            )}
            aria-expanded={isExpanded}
          >
            {isExpanded ? collapseLabel : expandLabel}
          </button>
        )}
      </>
    );
  },
);
 
Typography.displayName = "Typography";
 
export { Typography, SIZES };