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

fix(ElementLabel): allow element polymorphism UXD-1671 #1200

Merged
merged 1 commit into from
Nov 26, 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
42 changes: 24 additions & 18 deletions src/components/ElementLabel/ElementLabel.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ComponentPropsWithoutRef, ReactNode } from 'react';
import { ElementType, ReactNode, useMemo } from 'react';
import styled from 'styled-components';

import { WithAsProp } from '../../types/utils.types';
import { PolymorphicProps } from '../../types/utils.types';

export type ElementLabelProps = WithAsProp<{
type ElementLabelOwnProps = {
children: ReactNode;
/* Font size */
size?: 'sm' | 'md';
Expand All @@ -18,8 +18,11 @@ export type ElementLabelProps = WithAsProp<{
| 'link';
/* Font weight. When true ElementLabel will be rendered as bold */
isStrong?: boolean;
}> &
ComponentPropsWithoutRef<'span'>;
};
export type ElementLabelProps<Element extends ElementType> = PolymorphicProps<
ElementLabelOwnProps,
Element
>;

const ElementLabelRoot = styled.span`
font-family: var(--sscds-font-family-body);
Expand All @@ -30,24 +33,27 @@ const ElementLabelRoot = styled.span`
`;
ElementLabelRoot.displayName = 'ElementLabel.Root';

function ElementLabel({
function ElementLabel<E extends ElementType = 'span'>({
children,
size = 'sm',
color = 'default',
isStrong = false,
...props
}: ElementLabelProps) {
const vars = {
'--sscds-elementlabel-font-size': `var(--sscds-font-size-elementlabel-${size})`,
'--sscds-elementlabel-font-weight': isStrong
? 'var(--sscds-font-weight-elementlabel-strong)'
: 'var(--sscds-font-weight-elementlabel-default)',
'--sscds-elementlabel-color':
color === 'link'
? 'var(--sscds-color-link-default)'
: `var(--sscds-color-text-${color})`,
...props.style,
};
}: ElementLabelProps<E>) {
const vars = useMemo(
() => ({
'--sscds-elementlabel-font-size': `var(--sscds-font-size-elementlabel-${size})`,
'--sscds-elementlabel-font-weight': isStrong
? 'var(--sscds-font-weight-elementlabel-strong)'
: 'var(--sscds-font-weight-elementlabel-default)',
'--sscds-elementlabel-color':
color === 'link'
? 'var(--sscds-color-link-default)'
: `var(--sscds-color-text-${color})`,
...props.style,
}),
[color, isStrong, props.style, size],
);

return (
<ElementLabelRoot {...props} style={vars}>
Expand Down
12 changes: 9 additions & 3 deletions src/types/utils.types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ElementType } from 'react';
import { ComponentProps, ElementType } from 'react';

export type Subset<K> = {
[attr in keyof K]?: K[attr] extends object
Expand All @@ -18,8 +18,8 @@ export type RequireAtLeastOne<T, Keys extends keyof T = keyof T> = Pick<
[K in Keys]-?: Required<Pick<T, K>> & Partial<Pick<T, Exclude<Keys, K>>>;
}[Keys];

export type WithAsProp<Props> = Props & {
as?: ElementType;
export type WithAsProp<Props, Element = ElementType> = Props & {
as?: Element;
};

export type ReactComponentLike =
Expand All @@ -28,3 +28,9 @@ export type ReactComponentLike =
| ((props: any, context?: any) => any)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
| (new (props: any, context?: any) => any);

export type PolymorphicProps<
OwnProps,
Element extends ElementType,
> = WithAsProp<OwnProps, Element> &
Omit<ComponentProps<Element>, keyof WithAsProp<OwnProps, Element>>;
Loading