-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
30 changed files
with
428 additions
and
12 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
packages/components/src/components/Avatar/Avatar.module.css
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,34 @@ | ||
@import "@/styles"; | ||
.root { | ||
border-radius: var(--border-radius--round); | ||
width: var(--avatar-size); | ||
height: var(--avatar-size); | ||
overflow: hidden; | ||
} | ||
|
||
.initials { | ||
width: 100%; | ||
height: 100%; | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
font-size: calc(var(--avatar-size) * 0.4); | ||
} | ||
|
||
.image { | ||
width: 100%; | ||
height: 100%; | ||
object-fit: cover; | ||
} | ||
|
||
.size-s { | ||
--avatar-size: var(--avatar--size--s); | ||
} | ||
|
||
.size-m { | ||
--avatar-size: var(--avatar--size--m); | ||
} | ||
|
||
.size-l { | ||
--avatar-size: var(--avatar--size--l); | ||
} |
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,35 @@ | ||
import React, { FC, PropsWithChildren } from "react"; | ||
import styles from "./Avatar.module.css"; | ||
import clsx from "clsx"; | ||
import { PropsContext, PropsContextProvider } from "@/lib/propsContext"; | ||
|
||
interface AvatarProps extends PropsWithChildren { | ||
className?: string; | ||
/** @default "s" */ | ||
size?: "s" | "m" | "l"; | ||
} | ||
|
||
export const Avatar: FC<AvatarProps> = (props) => { | ||
const { children, className, size = "s" } = props; | ||
|
||
const rootClassName = clsx(className, styles.root, styles[`size-${size}`]); | ||
|
||
const propsContext: PropsContext = { | ||
Initials: { | ||
className: styles.initials, | ||
}, | ||
Image: { | ||
className: styles.image, | ||
}, | ||
}; | ||
|
||
return ( | ||
<div className={rootClassName}> | ||
<PropsContextProvider props={propsContext}> | ||
{children} | ||
</PropsContextProvider> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Avatar; |
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,3 @@ | ||
import { Avatar } from "./Avatar"; | ||
export { Avatar } from "./Avatar"; | ||
export default Avatar; |
37 changes: 37 additions & 0 deletions
37
packages/components/src/components/Avatar/stories/Default.stories.tsx
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,37 @@ | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
import Avatar from "../Avatar"; | ||
import React from "react"; | ||
import { Initials } from "@/components/Initials"; | ||
import { Image } from "@/components/Image"; | ||
import { dummyText } from "@/lib/dev/dummyText"; | ||
|
||
const meta: Meta<typeof Avatar> = { | ||
title: "Avatar", | ||
component: Avatar, | ||
render: (props) => ( | ||
<Avatar {...props}> | ||
<Image alt="Gopher" src={dummyText.imageSrc} /> | ||
</Avatar> | ||
), | ||
parameters: { | ||
controls: { exclude: ["className"] }, | ||
}, | ||
argTypes: { | ||
size: { | ||
control: "inline-radio", | ||
}, | ||
}, | ||
}; | ||
export default meta; | ||
|
||
type Story = StoryObj<typeof Avatar>; | ||
|
||
export const Default: Story = {}; | ||
|
||
export const WithInitials: Story = { | ||
render: (props) => ( | ||
<Avatar {...props}> | ||
<Initials>Max Mustermann</Initials> | ||
</Avatar> | ||
), | ||
}; |
17 changes: 17 additions & 0 deletions
17
packages/components/src/components/Avatar/stories/Variants.stories.tsx
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,17 @@ | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
import Avatar from "../Avatar"; | ||
import defaultMeta from "./Default.stories"; | ||
|
||
const meta: Meta<typeof Avatar> = { | ||
...defaultMeta, | ||
title: "Avatar/Variants", | ||
}; | ||
export default meta; | ||
|
||
type Story = StoryObj<typeof Avatar>; | ||
|
||
export const SizeS: Story = {}; | ||
|
||
export const SizeM: Story = { args: { size: "m" } }; | ||
|
||
export const SizeL: Story = { args: { size: "l" } }; |
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
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,4 @@ | ||
@import "@/styles"; | ||
|
||
.root { | ||
} |
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,16 @@ | ||
import React, { ComponentProps, FC } from "react"; | ||
import styles from "./Image.module.css"; | ||
import clsx from "clsx"; | ||
import { useProps } from "@/lib/propsContext"; | ||
|
||
export interface ImageProps extends ComponentProps<"img"> {} | ||
|
||
export const Image: FC<ImageProps> = (props) => { | ||
const { className, ...rest } = useProps("Image", props); | ||
|
||
const rootClassName = clsx(className, styles.root); | ||
|
||
return <img className={rootClassName} {...rest} />; | ||
}; | ||
|
||
export default Image; |
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,3 @@ | ||
import { Image } from "./Image"; | ||
export { type ImageProps, Image } from "./Image"; | ||
export default Image; |
15 changes: 15 additions & 0 deletions
15
packages/components/src/components/Image/stories/Default.stories.tsx
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,15 @@ | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
import Image from "../Image"; | ||
import React from "react"; | ||
import { dummyText } from "@/lib/dev/dummyText"; | ||
|
||
const meta: Meta<typeof Image> = { | ||
title: "Image", | ||
component: Image, | ||
render: (props) => <Image {...props} alt="Gopher" src={dummyText.imageSrc} />, | ||
}; | ||
export default meta; | ||
|
||
type Story = StoryObj<typeof Image>; | ||
|
||
export const Default: Story = {}; |
43 changes: 43 additions & 0 deletions
43
packages/components/src/components/Initials/Initials.module.css
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,43 @@ | ||
@import "@/styles"; | ||
|
||
.root { | ||
display: flex; | ||
font-weight: bold; | ||
background-color: var(--background-color); | ||
} | ||
|
||
.root > *:nth-child(odd) { | ||
color: var(--char-1-color); | ||
} | ||
|
||
.root > *:nth-child(even) { | ||
color: var(--char-2-color); | ||
} | ||
|
||
.root > *:only-child { | ||
color: var(--char-2-color); | ||
} | ||
|
||
.variant-1 { | ||
--background-color: var(--initials--variant-1-background-color); | ||
--char-1-color: var(--initials--variant-1-first-initial-color); | ||
--char-2-color: var(--initials--variant-1-second-initial-color); | ||
} | ||
|
||
.variant-2 { | ||
--background-color: var(--initials--variant-2-background-color); | ||
--char-1-color: var(--initials--variant-2-first-initial-color); | ||
--char-2-color: var(--initials--variant-2-second-initial-color); | ||
} | ||
|
||
.variant-3 { | ||
--background-color: var(--initials--variant-3-background-color); | ||
--char-1-color: var(--initials--variant-3-first-initial-color); | ||
--char-2-color: var(--initials--variant-3-second-initial-color); | ||
} | ||
|
||
.variant-4 { | ||
--background-color: var(--initials--variant-4-background-color); | ||
--char-1-color: var(--initials--variant-4-first-initial-color); | ||
--char-2-color: var(--initials--variant-4-second-initial-color); | ||
} |
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,34 @@ | ||
import React, { FC, PropsWithChildren } from "react"; | ||
import { getVariantFromInitials } from "./lib/getVariantFromInitials"; | ||
import { getInitialsFromString } from "./lib/getInitialsFromString"; | ||
import styles from "./Initials.module.css"; | ||
import clsx from "clsx"; | ||
import { useProps } from "@/lib/propsContext"; | ||
|
||
export interface InitialsProps extends PropsWithChildren<{ children: string }> { | ||
className?: string; | ||
} | ||
|
||
export const Initials: FC<InitialsProps> = (props) => { | ||
const { children, className } = useProps("Initials", props); | ||
|
||
const initials = getInitialsFromString(children); | ||
|
||
const rootClassName = clsx( | ||
className, | ||
styles.root, | ||
styles[`variant-${getVariantFromInitials(initials)}`], | ||
); | ||
|
||
const initialsElements = initials.map((initial, index) => ( | ||
<span key={index}>{initial}</span> | ||
)); | ||
|
||
return ( | ||
<div aria-label={children} className={rootClassName}> | ||
{initialsElements} | ||
</div> | ||
); | ||
}; | ||
|
||
export default Initials; |
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,3 @@ | ||
import { Initials } from "./Initials"; | ||
export { type InitialsProps, Initials } from "./Initials"; | ||
export default Initials; |
16 changes: 16 additions & 0 deletions
16
packages/components/src/components/Initials/lib/getInitialsFromString.test.ts
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,16 @@ | ||
import { getInitialsFromString } from "./getInitialsFromString"; | ||
|
||
describe('"getInitialsFromString()', () => { | ||
test("does return empty array if string is empty", () => { | ||
expect(getInitialsFromString("")).toStrictEqual([]); | ||
}); | ||
|
||
test.each([ | ||
["Max Mustermann", "MM"], | ||
["Max & Mustermann", "MM"], | ||
["Max (Mustermann)", "MM"], | ||
["Max", "M"], | ||
])("builds correct initials for %o", (item, expectedResult) => { | ||
expect(getInitialsFromString(item).join("")).toBe(expectedResult); | ||
}); | ||
}); |
9 changes: 9 additions & 0 deletions
9
packages/components/src/components/Initials/lib/getInitialsFromString.ts
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,9 @@ | ||
export const getInitialsFromString = (initials: string): string[] => { | ||
return initials | ||
.replace(/[^\p{L}\s]/giu, "") | ||
.split(" ") | ||
.map((part) => part.trim()[0]) | ||
.filter((p) => p !== undefined) | ||
.map((char) => char.toUpperCase()) | ||
.slice(0, 2); | ||
}; |
18 changes: 18 additions & 0 deletions
18
packages/components/src/components/Initials/lib/getVariantFromInitials.test.ts
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,18 @@ | ||
import { getVariantFromInitials } from "./getVariantFromInitials"; | ||
|
||
describe('"getVariantFromInitials()', () => { | ||
test("does return 1 if array is empty", () => { | ||
expect(getVariantFromInitials([])).toStrictEqual(1); | ||
}); | ||
|
||
test.each([ | ||
[["A"], 2], | ||
[["B"], 3], | ||
[["C", "D"], 4], | ||
[["Z"], 3], | ||
[["Ä"], 1], | ||
[["1"], 2], | ||
])("does get correct variant for given initial", (item, expectedVariant) => { | ||
expect(getVariantFromInitials(item)).toBe(expectedVariant); | ||
}); | ||
}); |
6 changes: 6 additions & 0 deletions
6
packages/components/src/components/Initials/lib/getVariantFromInitials.ts
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,6 @@ | ||
export const getVariantFromInitials = (initials: string[]): number => { | ||
if (initials.length < 1) { | ||
return 1; | ||
} | ||
return (initials[0].charCodeAt(0) % 4) + 1; | ||
}; |
21 changes: 21 additions & 0 deletions
21
packages/components/src/components/Initials/stories/Default.stories.tsx
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,21 @@ | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
import Initials from "../Initials"; | ||
import React from "react"; | ||
|
||
const meta: Meta<typeof Initials> = { | ||
title: "Initials", | ||
component: Initials, | ||
render: (props) => <Initials {...props}>Max Mustermann</Initials>, | ||
parameters: { | ||
controls: { exclude: ["className"] }, | ||
}, | ||
}; | ||
export default meta; | ||
|
||
type Story = StoryObj<typeof Initials>; | ||
|
||
export const Default: Story = {}; | ||
|
||
export const OneLetter: Story = { | ||
render: (props) => <Initials {...props}>Max </Initials>, | ||
}; |
Oops, something went wrong.
ff4ea7c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coverage report for
./packages/components/
Test suite run success
49 tests passing in 9 suites.
Report generated by 🧪jest coverage report action from ff4ea7c