forked from kamp-us/monorepo
-
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.
feat(@kampus/ui-next): Add other Shadcn components (kamp-us#501)
# Description Adds shadcn components for our ui library. Added components: - Time ago - User avatar - Theme provider - Theme toggle - Social Media Buttons ### Checklist - [x] discord username: `username#0001` - [x] Closes kamp-us#412 - [x] Closes kamp-us#505 - [x] PR must be created for an issue from issues under "In progress" column from [our project board](https://github.com/orgs/kamp-us/projects/2/views/1). - [x] A descriptive and understandable title: The PR title should clearly describe the nature and purpose of the changes. The PR title should be the first thing displayed when the PR is opened. And it should follow the semantic commit rules, and should include the app/package/service name in the title. For example, a title like "docs(@kampus-apps/pano): Add README.md" can be used. - [x] Related file selection: Only relevant files should be touched and no other files should be affected. - [ ] I ran `npx turbo run` at the root of the repository, and build was successful. - [x] I installed the npm packages using `npm install --save-exact <package>` so my package is pinned to a specific npm version. Leave empty if no package was installed. Leave empty if no package was installed with this PR. ### How were these changes tested? Please describe the tests you did to test the changes you made. Please also specify your test configuration.
- Loading branch information
Showing
13 changed files
with
271 additions
and
16 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
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,19 +1,18 @@ | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
|
||
import { Avatar, AvatarFallback, AvatarImage } from "@kampus/ui-next/components/avatar"; | ||
import { UserAvatar } from "@kampus/ui-next/components/user-avatar"; | ||
|
||
const meta = { | ||
component: Avatar, | ||
} satisfies Meta<typeof Avatar>; | ||
component: UserAvatar, | ||
} satisfies Meta<typeof UserAvatar>; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof meta>; | ||
|
||
export const Default = { | ||
render: () => ( | ||
<Avatar> | ||
<AvatarImage src="https://github.com/kamp-us.png" alt="@kampus" /> | ||
<AvatarFallback>K</AvatarFallback> | ||
</Avatar> | ||
), | ||
render: ({ login, src }) => <UserAvatar login={login} src={src} />, | ||
args: { | ||
login: "kampus", | ||
src: "https://github.com/kamp-us.png", | ||
}, | ||
} satisfies Story; |
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,28 @@ | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
|
||
import { | ||
FacebookShare, | ||
RedditShare, | ||
TwitterShare, | ||
} from "@kampus/ui-next/components/social-media-buttons"; | ||
|
||
const meta = { | ||
component: TwitterShare || FacebookShare || RedditShare, | ||
} satisfies Meta<typeof TwitterShare>; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof meta>; | ||
|
||
export const Default = { | ||
args: { | ||
postUrl: "https://kampus.rs", | ||
size: 32, | ||
}, | ||
render: ({ postUrl, size }) => ( | ||
<div style={{ display: "flex", gap: "10px" }}> | ||
<FacebookShare postUrl={postUrl} size={size} /> | ||
<TwitterShare postUrl={postUrl} size={size} /> | ||
<RedditShare postUrl={postUrl} size={size} /> | ||
</div> | ||
), | ||
} satisfies Story; |
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,12 @@ | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
|
||
import { ThemeToggle } from "@kampus/ui-next/components/theme-toggle"; | ||
|
||
const meta = { | ||
component: ThemeToggle, | ||
} satisfies Meta<typeof ThemeToggle>; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof meta>; | ||
|
||
export const Default = {} satisfies Story; |
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 { TimeAgo } from "@kampus/ui-next/components/time-ago"; | ||
|
||
const meta = { | ||
component: TimeAgo, | ||
args: { | ||
date: new Date(), | ||
}, | ||
} satisfies Meta<typeof TimeAgo>; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof meta>; | ||
|
||
export const Default = {} satisfies Story; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,9 @@ | ||
"use client"; | ||
|
||
import * as React from "react"; | ||
import { ThemeProvider as NextThemesProvider } from "next-themes"; | ||
import { type ThemeProviderProps } from "next-themes/dist/types"; | ||
|
||
export function ThemeProvider({ children, ...props }: ThemeProviderProps) { | ||
return <NextThemesProvider {...props}>{children}</NextThemesProvider>; | ||
} |
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,38 @@ | ||
"use client"; | ||
|
||
import * as React from "react"; | ||
import { Moon, Sun } from "lucide-react"; | ||
import { useTheme } from "next-themes"; | ||
|
||
import { cn } from "../utils"; | ||
import { Button } from "./button"; | ||
import { | ||
DropdownMenu, | ||
DropdownMenuContent, | ||
DropdownMenuItem, | ||
DropdownMenuTrigger, | ||
} from "./dropdown-menu"; | ||
|
||
export function ThemeToggle() { | ||
const { setTheme } = useTheme(); | ||
|
||
const baseClasses = "h-[1.2rem] w-[1.2rem] transition all"; | ||
|
||
return ( | ||
<DropdownMenu> | ||
<DropdownMenuTrigger asChild> | ||
<Button variant="outline" size="sm"> | ||
<Sun className={cn(baseClasses, "rotate-0 scale-100 dark:-rotate-90 dark:scale-0")} /> | ||
<Moon | ||
className={cn(baseClasses, "absolute rotate-90 scale-0 dark:rotate-0 dark:scale-100")} | ||
/> | ||
</Button> | ||
</DropdownMenuTrigger> | ||
<DropdownMenuContent align="end"> | ||
<DropdownMenuItem onClick={() => setTheme("light")}>Light</DropdownMenuItem> | ||
<DropdownMenuItem onClick={() => setTheme("dark")}>Dark</DropdownMenuItem> | ||
<DropdownMenuItem onClick={() => setTheme("system")}>System</DropdownMenuItem> | ||
</DropdownMenuContent> | ||
</DropdownMenu> | ||
); | ||
} |
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,50 @@ | ||
"use client"; | ||
|
||
import { useEffect, useState } from "react"; | ||
|
||
const formatter = new Intl.RelativeTimeFormat("tr", { | ||
// numeric: "auto", | ||
}); | ||
|
||
const DIVISIONS = [ | ||
{ amount: 60, name: "seconds" }, | ||
{ amount: 60, name: "minutes" }, | ||
{ amount: 24, name: "hours" }, | ||
{ amount: 7, name: "days" }, | ||
{ amount: 4.34524, name: "weeks" }, | ||
{ amount: 12, name: "months" }, | ||
{ amount: Number.POSITIVE_INFINITY, name: "years" }, | ||
]; | ||
|
||
export function formatTimeAgo(date: Date) { | ||
const now = new Date(); | ||
let duration = (date.getTime() - now.getTime()) / 1000; | ||
|
||
for (let i = 0; i <= DIVISIONS.length; i++) { | ||
const division = DIVISIONS[i] as (typeof DIVISIONS)[number]; | ||
if (Math.abs(duration) < division.amount) { | ||
return formatter.format(Math.round(duration), division.name as Intl.RelativeTimeFormatUnit); | ||
} | ||
duration /= division.amount; | ||
} | ||
} | ||
|
||
interface TimeagoProps { | ||
date: Date; | ||
} | ||
|
||
export const TimeAgo = (props: TimeagoProps) => { | ||
const [timeAgo, setTimeAgo] = useState(formatTimeAgo(props.date)); | ||
|
||
useEffect(() => { | ||
const timer = setInterval(() => { | ||
setTimeAgo(formatTimeAgo(props.date)); | ||
}, 10000); | ||
|
||
return () => clearInterval(timer); | ||
}, [props.date]); | ||
|
||
// If we don't suppress the hydration warning, we get this error: https://nextjs.org/docs/messages/react-hydration-error | ||
// https://nextjs.org/docs/messages/react-hydration-error#solution-3-using-suppresshydrationwarning | ||
return <div suppressHydrationWarning>{timeAgo}</div>; | ||
}; |
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,23 @@ | ||
"use client"; | ||
|
||
import React, { forwardRef } from "react"; | ||
|
||
import { Avatar, AvatarFallback, AvatarImage } from "./avatar"; | ||
|
||
export interface UserAvatarProps { | ||
login: string; | ||
src?: string; | ||
} | ||
|
||
export const UserAvatar = forwardRef<HTMLSpanElement, UserAvatarProps>(({ login, src }, ref) => { | ||
const firstLetter = login.charAt(0).toUpperCase(); | ||
|
||
return ( | ||
<Avatar> | ||
<AvatarImage src={src} alt={login} /> | ||
<AvatarFallback ref={ref}>{firstLetter}</AvatarFallback> | ||
</Avatar> | ||
); | ||
}); | ||
|
||
UserAvatar.displayName = "UserAvatar"; |
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