From 4519494be8d552dd2bb84845bad8247ede86732a Mon Sep 17 00:00:00 2001 From: Andre H Date: Wed, 28 Feb 2024 13:49:11 +0700 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20refactor:=20cherry=20pick?= =?UTF-8?q?=20fomr=20main?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../shared/Heading/Heading.theme.ts | 7 +++++ .../src/components/shared/Heading/Heading.tsx | 30 +++++++++++++++++++ .../src/components/shared/Heading/index.ts | 1 + packages/frontend/src/types/common.ts | 18 +++++++++++ 4 files changed, 56 insertions(+) create mode 100644 packages/frontend/src/components/shared/Heading/Heading.theme.ts create mode 100644 packages/frontend/src/components/shared/Heading/Heading.tsx create mode 100644 packages/frontend/src/components/shared/Heading/index.ts diff --git a/packages/frontend/src/components/shared/Heading/Heading.theme.ts b/packages/frontend/src/components/shared/Heading/Heading.theme.ts new file mode 100644 index 00000000..5ce5bb5c --- /dev/null +++ b/packages/frontend/src/components/shared/Heading/Heading.theme.ts @@ -0,0 +1,7 @@ +import { tv, type VariantProps } from 'tailwind-variants'; + +export const headingTheme = tv({ + base: ['text-elements-high-em', 'font-display', 'font-normal'], +}); + +export type HeadingVariants = VariantProps; diff --git a/packages/frontend/src/components/shared/Heading/Heading.tsx b/packages/frontend/src/components/shared/Heading/Heading.tsx new file mode 100644 index 00000000..0621039f --- /dev/null +++ b/packages/frontend/src/components/shared/Heading/Heading.tsx @@ -0,0 +1,30 @@ +import React, { type PropsWithChildren } from 'react'; + +import { headingTheme, type HeadingVariants } from './Heading.theme'; +import { PolymorphicProps } from 'types/common'; + +export type HeadingElementType = 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6'; + +export type HeadingProps = + PropsWithChildren>; + +export const Heading = ({ + children, + as, + className, + ...props +}: HeadingProps) => { + // Component is the element that will be rendered + const Component = as ?? 'h1'; + + return ( + + {children} + + ); +}; diff --git a/packages/frontend/src/components/shared/Heading/index.ts b/packages/frontend/src/components/shared/Heading/index.ts new file mode 100644 index 00000000..6406e7b0 --- /dev/null +++ b/packages/frontend/src/components/shared/Heading/index.ts @@ -0,0 +1 @@ +export * from './Heading'; diff --git a/packages/frontend/src/types/common.ts b/packages/frontend/src/types/common.ts index fc796289..dd0fc09f 100644 --- a/packages/frontend/src/types/common.ts +++ b/packages/frontend/src/types/common.ts @@ -1,3 +1,9 @@ +import { + type ElementType, + type ComponentPropsWithoutRef, + forwardRef, +} from 'react'; + /** * Construct a type by excluding common keys from one type to another. * @template T - The type from which to omit properties. @@ -7,3 +13,15 @@ * @returns A new type that includes all properties from T except those that are common with U. */ export type OmitCommon = Pick>; + +export type PolymorphicProps = Props & + Omit, 'as'> & { + as?: Element; + }; + +// taken from : https://github.com/total-typescript/react-typescript-tutorial/blob/main/src/08-advanced-patterns/72-as-prop-with-forward-ref.solution.tsx +type FixedForwardRef = ( + render: (props: P, ref: React.Ref) => React.ReactNode, +) => (props: P & React.RefAttributes) => JSX.Element; + +export const fixedForwardRef = forwardRef as FixedForwardRef;