-
Notifications
You must be signed in to change notification settings - Fork 672
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add AutoResizeComponent and entry content header for metadata (#72
) * feat: add component Signed-off-by: Innei <i@innei.in> * feat: entry title meta Signed-off-by: Innei <i@innei.in> * fix: update anchor Signed-off-by: Innei <i@innei.in> * fix: ts error Signed-off-by: Innei <i@innei.in> --------- Signed-off-by: Innei <i@innei.in>
- Loading branch information
Showing
12 changed files
with
409 additions
and
70 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
"use client" | ||
|
||
import type { JSX } from "react" | ||
import { cloneElement } from "react" | ||
|
||
export const ProviderComposer: Component<{ | ||
contexts: JSX.Element[] | ||
}> = ({ contexts, children }) => | ||
contexts.reduceRight( | ||
(kids: any, parent: any) => cloneElement(parent, { children: kids }), | ||
Check warning on line 10 in src/renderer/src/components/common/ProviderComposer.tsx GitHub Actions / Lint and Typecheck (18.x)
Check warning on line 10 in src/renderer/src/components/common/ProviderComposer.tsx GitHub Actions / Lint and Typecheck (18.x)
|
||
children, | ||
) |
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,57 @@ | ||
import { cn } from "@renderer/lib/utils" | ||
import type { Spring } from "framer-motion" | ||
import { m } from "framer-motion" | ||
import { useEffect, useRef, useState } from "react" | ||
|
||
const softSpringPreset: Spring = { | ||
duration: 0.35, | ||
type: "spring", | ||
stiffness: 120, | ||
damping: 20, | ||
} | ||
|
||
interface AnimateChangeInHeightProps { | ||
children: React.ReactNode | ||
className?: string | ||
duration?: number | ||
|
||
spring?: boolean | ||
} | ||
|
||
export const AutoResizeHeight: React.FC<AnimateChangeInHeightProps> = ({ | ||
children, | ||
className, | ||
duration = 0.6, | ||
spring = false, | ||
}) => { | ||
const containerRef = useRef<HTMLDivElement | null>(null) | ||
const [height, setHeight] = useState<number | "auto">("auto") | ||
|
||
useEffect(() => { | ||
if (!containerRef.current) return | ||
const resizeObserver = new ResizeObserver((entries) => { | ||
// We only have one entry, so we can use entries[0]. | ||
const observedHeight = entries[0].contentRect.height | ||
setHeight(observedHeight) | ||
}) | ||
|
||
resizeObserver.observe(containerRef.current) | ||
|
||
return () => { | ||
// Cleanup the observer when the component is unmounted | ||
resizeObserver.disconnect() | ||
} | ||
}, []) | ||
|
||
return ( | ||
<m.div | ||
className={cn("overflow-hidden", className)} | ||
style={{ height }} | ||
initial={false} | ||
animate={{ height }} | ||
transition={spring ? softSpringPreset : { duration }} | ||
> | ||
<div ref={containerRef}>{children}</div> | ||
</m.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
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,14 @@ | ||
/* eslint-disable unicorn/no-unreadable-array-destructuring */ | ||
|
||
import { createAtomHooks } from "@renderer/lib/jotai" | ||
import { atom } from "jotai" | ||
|
||
export const [, , useEntryTitleMeta, , getEntryTitleMeta, setEntryTitleMeta] = | ||
createAtomHooks( | ||
atom( | ||
null as Nullable<{ | ||
title: string | ||
description: string | ||
}>, | ||
), | ||
) |
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,70 @@ | ||
import { ActionButton } from "@renderer/components/ui/button" | ||
import { useEntryActions } from "@renderer/hooks" | ||
import { cn } from "@renderer/lib/utils" | ||
import { useEntry } from "@renderer/store/entry/hooks" | ||
import { AnimatePresence, m } from "framer-motion" | ||
|
||
import { useEntryTitleMeta } from "./atoms" | ||
|
||
export function EntryHeader({ | ||
view, | ||
entryId, | ||
}: { | ||
view: number | ||
entryId: string | ||
}) { | ||
const entry = useEntry(entryId) | ||
|
||
const { items } = useEntryActions({ | ||
view, | ||
entry, | ||
}) | ||
const entryTitleMeta = useEntryTitleMeta() | ||
if (!entry?.entries.url) return null | ||
|
||
return ( | ||
<div | ||
className={cn( | ||
"flex h-[55px] min-w-0 items-center justify-between gap-3 overflow-hidden px-5 text-lg text-zinc-500", | ||
entryTitleMeta && "border-b border-border", | ||
)} | ||
> | ||
<div className="flex min-w-0 shrink"> | ||
<AnimatePresence> | ||
{entryTitleMeta && ( | ||
<m.div | ||
initial={{ opacity: 0.01, y: 30 }} | ||
animate={{ opacity: 1, y: 0 }} | ||
exit={{ opacity: 0.01, y: 30 }} | ||
className="flex min-w-0 shrink items-end gap-2 truncate text-sm leading-tight text-theme-foreground" | ||
> | ||
<span className="min-w-0 shrink truncate font-bold">{entryTitleMeta.title}</span> | ||
<i className="i-mingcute-line-line size-[10px] shrink-0 translate-y-[-3px] rotate-[-25deg]" /> | ||
<span className="shrink-0 truncate text-xs opacity-80"> | ||
{entryTitleMeta.description} | ||
</span> | ||
</m.div> | ||
)} | ||
</AnimatePresence> | ||
</div> | ||
<div className="flex items-center gap-3"> | ||
{items | ||
.filter((item) => !item.disabled) | ||
.map((item) => ( | ||
<ActionButton | ||
icon={ | ||
item.icon ? ( | ||
<img className="size-4 grayscale" src={item.icon} /> | ||
) : ( | ||
<i className={item.className} /> | ||
) | ||
} | ||
onClick={item.onClick} | ||
tooltip={item.name} | ||
key={item.name} | ||
/> | ||
))} | ||
</div> | ||
</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
Oops, something went wrong.