-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
- Loading branch information
Showing
5 changed files
with
137 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@telegraph/typography": patch | ||
--- | ||
|
||
Adds text component, fixes className persistence issue |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 as="p">Text</Text>); | ||
expect(await axe(container)).toHaveNoViolations(); | ||
}); | ||
it("size props applies correct className", () => { | ||
const { container } = render( | ||
<Text size="9" as="p"> | ||
Text | ||
</Text>, | ||
); | ||
expect(container.firstChild).toHaveClass("text-9"); | ||
}); | ||
it("color props applies correct className", () => { | ||
const { container } = render( | ||
<Text color="red" as="p"> | ||
Text | ||
</Text>, | ||
); | ||
expect(container.firstChild).toHaveClass("text-red-11"); | ||
}); | ||
it("align props applies correct className", () => { | ||
const { container } = render( | ||
<Text align="left" as="p"> | ||
Text | ||
</Text>, | ||
); | ||
expect(container.firstChild).toHaveClass("text-left"); | ||
}); | ||
it("weight props applies correct className", () => { | ||
const { container } = render( | ||
<Text weight="medium" as="p"> | ||
Text | ||
</Text>, | ||
); | ||
expect(container.firstChild).toHaveClass("font-medium"); | ||
}); | ||
it("default props applies correct className", () => { | ||
const { container } = render(<Text as="p">Text</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"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,59 @@ | ||
function Text() { | ||
return <p className="text-amber-400">Text</p>; | ||
} | ||
import clsx from "clsx"; | ||
import React from "react"; | ||
|
||
import { | ||
alignMap, | ||
colorMap, | ||
sizeMap, | ||
weightMap, | ||
} from "../helpers/prop-mappings"; | ||
|
||
type TextProps = React.HTMLAttributes<HTMLElement> & { | ||
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<TextRef, TextProps>( | ||
( | ||
{ | ||
color = "black", | ||
size = "2", | ||
weight = "regular", | ||
align, | ||
className, | ||
...props | ||
}, | ||
forwardedRef, | ||
) => { | ||
return ( | ||
<span | ||
className={clsx( | ||
align && alignMap[align], | ||
color && colorMap[color], | ||
size && sizeMap[size], | ||
weight && weightMap[weight], | ||
className, | ||
)} | ||
ref={forwardedRef} | ||
{...props} | ||
/> | ||
); | ||
}, | ||
); | ||
|
||
export { Text }; |