From befcb4f61375e2c9460e2df8af4512122c22891d Mon Sep 17 00:00:00 2001 From: Kyle McDonald Date: Wed, 7 Feb 2024 12:07:16 -0600 Subject: [PATCH] feat: text component (#34) * fix: add required as tag to heading test * fix: pass classname through to heading * fix: format heading test * feat: text component * docs(changeset): add changeset * fix: correct type for html attributes --- .changeset/funny-papayas-wink.md | 5 ++ .../typography/src/Heading/Heading.test.tsx | 22 +++++-- packages/typography/src/Heading/Heading.tsx | 6 +- packages/typography/src/Text/Text.test.tsx | 53 ++++++++++++++++ packages/typography/src/Text/Text.tsx | 60 ++++++++++++++++++- 5 files changed, 137 insertions(+), 9 deletions(-) create mode 100644 .changeset/funny-papayas-wink.md create mode 100644 packages/typography/src/Text/Text.test.tsx diff --git a/.changeset/funny-papayas-wink.md b/.changeset/funny-papayas-wink.md new file mode 100644 index 00000000..97c0af00 --- /dev/null +++ b/.changeset/funny-papayas-wink.md @@ -0,0 +1,5 @@ +--- +"@telegraph/typography": patch +--- + +Adds text component, fixes className persistence issue diff --git a/packages/typography/src/Heading/Heading.test.tsx b/packages/typography/src/Heading/Heading.test.tsx index 9eea5f07..2e29cf98 100644 --- a/packages/typography/src/Heading/Heading.test.tsx +++ b/packages/typography/src/Heading/Heading.test.tsx @@ -7,23 +7,35 @@ import { Heading } from "./Heading"; describe("Heading", () => { it("should render without a11y issues", async () => { - const { container } = render(Heading); + const { container } = render(Heading); expect(await axe(container)).toHaveNoViolations(); }); it("size props applies correct className", () => { - const { container } = render(Heading); + const { container } = render( + + Heading + , + ); expect(container.firstChild).toHaveClass("text-9"); }); it("color props applies correct className", () => { - const { container } = render(Heading); + const { container } = render( + + Heading + , + ); expect(container.firstChild).toHaveClass("text-red-11"); }); it("align props applies correct className", () => { - const { container } = render(Heading); + const { container } = render( + + Heading + , + ); expect(container.firstChild).toHaveClass("text-left"); }); it("default props applies correct className", () => { - const { container } = render(Heading); + const { container } = render(Heading); expect(container.firstChild).toHaveClass("text-gray-12"); expect(container.firstChild).toHaveClass("text-2"); expect(container.firstChild).toHaveClass("leading-2"); diff --git a/packages/typography/src/Heading/Heading.tsx b/packages/typography/src/Heading/Heading.tsx index 831d45c5..39d1e127 100644 --- a/packages/typography/src/Heading/Heading.tsx +++ b/packages/typography/src/Heading/Heading.tsx @@ -13,7 +13,10 @@ type HeadingProps = React.HTMLAttributes & { type HeadingRef = HTMLHeadingElement; const Heading = React.forwardRef( - ({ color = "black", size = "2", align, ...props }, forwardedRef) => { + ( + { color = "black", size = "2", align, className, ...props }, + forwardedRef, + ) => { return (

( color && colorMap[color], size && sizeMap[size], "font-semi-bold", + className, )} ref={forwardedRef} {...props} diff --git a/packages/typography/src/Text/Text.test.tsx b/packages/typography/src/Text/Text.test.tsx new file mode 100644 index 00000000..d21af472 --- /dev/null +++ b/packages/typography/src/Text/Text.test.tsx @@ -0,0 +1,53 @@ +import { render } from "@testing-library/react"; +import React from "react"; +import { describe, expect, it } from "vitest"; +import { axe } from "vitest-axe"; + +import { Text } from "./Text"; + +describe("Text", () => { + it("should render without a11y issues", async () => { + const { container } = render(Text); + expect(await axe(container)).toHaveNoViolations(); + }); + it("size props applies correct className", () => { + const { container } = render( + + Text + , + ); + expect(container.firstChild).toHaveClass("text-9"); + }); + it("color props applies correct className", () => { + const { container } = render( + + Text + , + ); + expect(container.firstChild).toHaveClass("text-red-11"); + }); + it("align props applies correct className", () => { + const { container } = render( + + Text + , + ); + expect(container.firstChild).toHaveClass("text-left"); + }); + it("weight props applies correct className", () => { + const { container } = render( + + Text + , + ); + expect(container.firstChild).toHaveClass("font-medium"); + }); + it("default props applies correct className", () => { + const { container } = render(Text); + expect(container.firstChild).toHaveClass("text-gray-12"); + expect(container.firstChild).toHaveClass("text-2"); + expect(container.firstChild).toHaveClass("leading-2"); + expect(container.firstChild).toHaveClass("tracking-2"); + expect(container.firstChild).toHaveClass("font-regular"); + }); +}); diff --git a/packages/typography/src/Text/Text.tsx b/packages/typography/src/Text/Text.tsx index a4f6af20..e8d0e7d7 100644 --- a/packages/typography/src/Text/Text.tsx +++ b/packages/typography/src/Text/Text.tsx @@ -1,5 +1,59 @@ -function Text() { - return

Text

; -} +import clsx from "clsx"; +import React from "react"; + +import { + alignMap, + colorMap, + sizeMap, + weightMap, +} from "../helpers/prop-mappings"; + +type TextProps = React.HTMLAttributes & { + as: + | "p" + | "span" + | "div" + | "label" + | "em" + | "strong" + | "b" + | "i" + | "pre" + | "code"; + align?: keyof typeof alignMap; + size?: keyof typeof sizeMap; + color?: keyof typeof colorMap; + weight?: keyof typeof weightMap; +}; + +type TextRef = HTMLElement; + +const Text = React.forwardRef( + ( + { + color = "black", + size = "2", + weight = "regular", + align, + className, + ...props + }, + forwardedRef, + ) => { + return ( + + ); + }, +); export { Text };