Skip to content

Commit

Permalink
Update <Text /> to forwardRef and support href for links
Browse files Browse the repository at this point in the history
  • Loading branch information
calebjacob committed Nov 8, 2024
1 parent 77aeac8 commit c252d6e
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 57 deletions.
4 changes: 2 additions & 2 deletions src/components/Button.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ComponentPropsWithRef, ReactElement } from 'react';
import type { ComponentProps, ComponentPropsWithRef, ReactElement } from 'react';
import { forwardRef } from 'react';

import { usePagodaUi } from '../context/PagodaUi';
Expand All @@ -13,7 +13,7 @@ type Props = Omit<ComponentPropsWithRef<'button'>, 'size'> & {
count?: number;
fill?: Fill;
href?: string;
target?: ComponentPropsWithRef<'a'>['target'];
target?: ComponentProps<'a'>['target'];
icon?: ReactElement;
iconLeft?: ReactElement;
iconRight?: ReactElement;
Expand Down
2 changes: 2 additions & 0 deletions src/components/Text.README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { Text } from '~/components/lib/Text';
<Text size="text-xs">My Text</Text>
<Text as="h1">My Text</Text>
<Text as="h2" size="text-s" color="red-8">My Text</Text>
<Text href="/foo/bar">My Text</Text>
<Text href="/foo/bar" target="_blank">My Text</Text>
```

## Line Clamp
Expand Down
10 changes: 8 additions & 2 deletions src/components/Text.module.scss
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
a.text {
&:hover,
&:focus-visible {
color: var(--violet-11) !important;
text-decoration: underline;
}
}

.text {
text-wrap: pretty;
text-underline-offset: 0.2em;
Expand All @@ -17,11 +25,9 @@
a > & {
display: inline;
}

a:has(&) {
line-height: 0;
}

&[role='button']:hover,
&[role='button']:focus-visible,
[role='button']:hover > &,
Expand Down
114 changes: 61 additions & 53 deletions src/components/Text.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { type CSSProperties, type MouseEventHandler, type ReactNode } from 'react';
import { ComponentProps, ComponentPropsWithRef, type CSSProperties, forwardRef } from 'react';

import { usePagodaUi } from '../context/PagodaUi';
import { type ThemeColor, type ThemeFontSize } from '../utils/theme';
import s from './Text.module.scss';

Expand All @@ -17,71 +18,78 @@ const defaultSizes: Record<Tag, ThemeFontSize> = {
label: 'text-base',
};

type Props = {
type Props = Omit<ComponentPropsWithRef<'p'>, 'color'> & {
as?: Tag;
children: ReactNode;
clampLines?: number;
className?: string;
clickableHighlight?: boolean;
color?: ThemeColor;
decoration?: CSSProperties['textDecoration'];
family?: 'standard' | 'monospace';
forceWordBreak?: boolean;
id?: string;
href?: string;
target?: ComponentProps<'a'>['target'];
size?: ThemeFontSize;
sizePhone?: ThemeFontSize;
sizeTablet?: ThemeFontSize;
style?: CSSProperties;
noWrap?: boolean;
onClick?: MouseEventHandler<HTMLElement>;
weight?: string | number;
uppercase?: boolean;
};

export const Text = ({
as = 'p',
children,
clampLines,
className = '',
clickableHighlight,
color,
decoration,
family,
forceWordBreak,
size,
style,
weight,
noWrap,
onClick,
uppercase,
...props
}: Props) => {
const Tag = as;
const defaultSize = defaultSizes[as];
export const Text = forwardRef<HTMLParagraphElement, Props>(
(
{
as = 'p',
children,
clampLines,
className = '',
clickableHighlight,
color,
decoration,
family,
forceWordBreak,
size,
style,
weight,
noWrap,
onClick,
uppercase,
...props
},
ref,
) => {
const Tag = as;
const defaultSize = defaultSizes[as];
const { Link } = usePagodaUi();
const Element: any = props.href ? Link : Tag;

return (
<Tag
className={`${s.text} ${className}`}
data-clamp-lines={clampLines}
data-size={size ?? defaultSize}
data-clickable-highlight={clickableHighlight}
data-family={family}
role={onClick ? 'button' : undefined}
tabIndex={onClick ? 0 : undefined}
style={{
color: color ? (color === 'current' ? 'currentColor' : `var(--${color})`) : undefined,
textDecoration: decoration,
fontWeight: weight,
WebkitLineClamp: clampLines,
whiteSpace: noWrap ? 'nowrap' : undefined,
wordBreak: forceWordBreak ? 'break-word' : undefined,
textTransform: uppercase ? 'uppercase' : undefined,
...style,
}}
onClick={onClick}
{...props}
>
{children}
</Tag>
);
};
return (
<Element
className={`${s.text} ${className}`}
data-clamp-lines={clampLines}
data-size={size ?? defaultSize}
data-clickable-highlight={clickableHighlight}
data-family={family}
role={onClick ? 'button' : undefined}
tabIndex={onClick ? 0 : undefined}
style={{
color: color ? (color === 'current' ? 'currentColor' : `var(--${color})`) : undefined,
textDecoration: decoration,
fontWeight: weight,
WebkitLineClamp: clampLines,
whiteSpace: noWrap ? 'nowrap' : undefined,
wordBreak: forceWordBreak ? 'break-word' : undefined,
textTransform: uppercase ? 'uppercase' : undefined,
...style,
}}
onClick={onClick}
ref={ref}
{...props}
>
{children}
</Element>
);
},
);

Text.displayName = 'Text';

0 comments on commit c252d6e

Please sign in to comment.