Skip to content

Commit dda21d2

Browse files
authored
feat(Heading): Add asChild support to Heading (#1459)
1 parent 54b1dab commit dda21d2

File tree

1 file changed

+18
-2
lines changed
  • packages/react/src/components/Typography/Heading

1 file changed

+18
-2
lines changed

packages/react/src/components/Typography/Heading/Heading.tsx

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { ElementType, HTMLAttributes } from 'react';
22
import React, { forwardRef } from 'react';
33
import cl from 'clsx';
4+
import { Slot } from '@radix-ui/react-slot';
45

56
import type { OverridableComponent } from '../../../types/OverridableComponent';
67

@@ -15,16 +16,31 @@ export type HeadingProps = {
1516
size?: 'xxsmall' | 'xsmall' | 'small' | 'medium' | 'large' | 'xlarge';
1617
/** Adds margin-bottom */
1718
spacing?: boolean;
19+
/**
20+
* Change the default rendered element for the one passed as a child, merging their props and behavior.
21+
* @default false
22+
*/
23+
asChild?: boolean;
1824
} & HTMLAttributes<HTMLHeadingElement>;
1925

2026
/** Use `Heading` to render h1-6 elements with heading text styles. */
2127
export const Heading: OverridableComponent<HeadingProps, HTMLHeadingElement> =
2228
forwardRef(
2329
(
24-
{ level = 1, size = 'xlarge', spacing = false, className, as, ...rest },
30+
{
31+
level = 1,
32+
size = 'xlarge',
33+
spacing = false,
34+
className,
35+
as,
36+
asChild,
37+
...rest
38+
},
2539
ref,
2640
) => {
27-
const Component = as ?? (`h${level ?? 1}` as ElementType);
41+
const Component = asChild
42+
? Slot
43+
: as ?? (`h${level ?? 1}` as ElementType);
2844

2945
return (
3046
<Component

0 commit comments

Comments
 (0)