From d04818ccd8de2f136751acd890f8741b5bf7475d Mon Sep 17 00:00:00 2001 From: Razboy20 Date: Fri, 15 Mar 2024 22:06:36 -0500 Subject: [PATCH] refactor: update text and link components to be polymorphic (#171) --- .../components/calendar/ImportantLinks.tsx | 12 +++-- src/views/components/common/Link/Link.tsx | 19 +++---- src/views/components/common/Text/Text.tsx | 53 ++++++++++++------- .../RecruitmentBanner/RecruitmentBanner.tsx | 4 +- unocss.config.ts | 5 +- 5 files changed, 55 insertions(+), 38 deletions(-) diff --git a/src/views/components/calendar/ImportantLinks.tsx b/src/views/components/calendar/ImportantLinks.tsx index 6cdce39a..2a8125eb 100644 --- a/src/views/components/calendar/ImportantLinks.tsx +++ b/src/views/components/calendar/ImportantLinks.tsx @@ -15,12 +15,16 @@ interface LinkItem { const links: LinkItem[] = [ { - text: "Summer '24 Course Schedule", - url: 'https://utdirect.utexas.edu/apps/registrar/course_schedule/20242/', + text: "Fall '24 Course Schedule", + url: 'https://utdirect.utexas.edu/apps/registrar/course_schedule/20249/', }, { - text: "Fall '24 Course Schedule", - url: 'https://utdirect.utexas.edu/apps/registrar/course_schedule/20236/', + text: "Summer '24 Course Schedule", + url: 'https://utdirect.utexas.edu/apps/registrar/course_schedule/20246/', + }, + { + text: "Spring '24 Course Schedule", + url: 'https://utdirect.utexas.edu/apps/registrar/course_schedule/20242/', }, { text: 'Registration Info Sheet', diff --git a/src/views/components/common/Link/Link.tsx b/src/views/components/common/Link/Link.tsx index 84db883d..07cfef4d 100644 --- a/src/views/components/common/Link/Link.tsx +++ b/src/views/components/common/Link/Link.tsx @@ -7,31 +7,28 @@ import React from 'react'; import styles from './Link.module.scss'; -type Props = Omit & { - url?: string; +type Props = TextProps<'a'> & { + href?: string; disabled?: boolean; - title?: string; }; /** * A reusable Text component with props that build on top of the design system for the extension */ export default function Link(props: PropsWithChildren): JSX.Element { - let passedProps = { - ...props, - }; - const { url } = props; + let { className, href, ...passedProps } = props; - if (url && !props.onClick) { - passedProps.onClick = () => background.openNewTab({ url }); + if (href && !props.onClick) { + passedProps.onClick = () => background.openNewTab({ url: href }); } - const isDisabled = props.disabled || (!url && !props.onClick); + const isDisabled = props.disabled || (!href && !props.onClick); return ( & { as?: 'span' }) - | (React.HTMLAttributes & { as: 'div' }) -); +type PropsWeControl = 'as' | 'children'; +type CleanProps = Omit< + PropsOf, + TOmitableProps | PropsWeControl +>; +type OurProps = { + as?: TTag; + children?: ReactNode; +}; + +type AsProps = CleanProps & OurProps & TOverrides; const variants = ['mini', 'small', 'p', 'h4', 'h3-course', 'h3', 'h2-course', 'h2', 'h1-course', 'h1'] as const; type Variant = (typeof variants)[number]; +/** + * Props for the Text component. + */ +export type TextProps = PropsOf['className'] extends string + ? AsProps< + TTag, + { + variant?: Variant; + } + > + : never; + /** * A reusable Text component with props that build on top of the design system for the extension */ -export default function Text({ variant, as, className, ...props }: PropsWithChildren): JSX.Element { - const mergedClassName = clsx(styles.text, styles[variant], className); +export default function Text({ + as, + className, + variant, + ...rest +}: TextProps): JSX.Element { + const Comp = as || 'span'; + const mergedClassName = clsx(styles.text, styles[variant || 'p'], className); - if (as === 'div') return
; - - return ( - - {props.children} - - ); + return ; } diff --git a/src/views/components/injected/RecruitmentBanner/RecruitmentBanner.tsx b/src/views/components/injected/RecruitmentBanner/RecruitmentBanner.tsx index 4944c19c..923e0ff6 100644 --- a/src/views/components/injected/RecruitmentBanner/RecruitmentBanner.tsx +++ b/src/views/components/injected/RecruitmentBanner/RecruitmentBanner.tsx @@ -37,11 +37,11 @@ export default function RecruitmentBanner(): JSX.Element {
Interested in helping us develop UT Registration Plus? Check out our{' '} - + Discord Server {' '} and{' '} - + GitHub ! diff --git a/unocss.config.ts b/unocss.config.ts index 2d025e5c..0493bd75 100644 --- a/unocss.config.ts +++ b/unocss.config.ts @@ -10,7 +10,7 @@ export default defineConfig({ rules: [ [ 'btn-transition', - { transition: 'color 180ms, border-color 150ms, background-color 150ms, box-shadow 100ms, transform 50ms' }, + { transition: 'color 180ms, border-color 150ms, background-color 150ms, box-shadow 0ms, transform 50ms' }, ], [ 'ring-offset-0', @@ -21,7 +21,8 @@ export default defineConfig({ ], shortcuts: { focusable: 'outline-none ring-blue-500/50 dark:ring-blue-400/60 ring-0 focus-visible:ring-4', - btn: 'h-10 w-auto flex cursor-pointer justify-center items-center gap-2 rounded-1 px-4 py-0 text-4.5 btn-transition btn-transition disabled:(cursor-not-allowed opacity-50) active:enabled:scale-96 focusable', + btn: 'h-10 w-auto flex cursor-pointer justify-center items-center gap-2 rounded-1 px-4 py-0 text-4.5 btn-transition disabled:(cursor-not-allowed opacity-50) active:enabled:scale-96 focusable', + link: 'text-ut-burntorange underline underline-offset-2 hover:text-ut-orange focus-visible:text-ut-orange focusable btn-transition ease-out-expo', }, theme: { easing: {