Skip to content

Commit

Permalink
feat: toc item smooth indicator
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <tukon479@gmail.com>
  • Loading branch information
Innei committed Jun 16, 2023
1 parent e4fdd9a commit d459308
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
3 changes: 3 additions & 0 deletions src/app/notes/[id]/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import RemoveMarkdown from 'remove-markdown'
import type { Metadata } from 'next'

import { BottomToUpTransitionView } from '~/components/ui/transition/BottomToUpTransitionView'
import { attachUA } from '~/lib/attach-ua'
import { queries } from '~/queries/definition'
import { getQueryClient } from '~/utils/query-client.server'

Expand All @@ -15,6 +16,7 @@ export const generateMetadata = async ({
}
}): Promise<Metadata> => {
try {
attachUA()
const { data } = await getQueryClient().fetchQuery(
queries.note.byNid(params.id),
)
Expand Down Expand Up @@ -52,6 +54,7 @@ export default async (
id: string
}>,
) => {
attachUA()
await getQueryClient().prefetchQuery(queries.note.byNid(props.params.id))
return (
<BottomToUpTransitionView>
Expand Down
12 changes: 10 additions & 2 deletions src/components/widgets/toc/Toc.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { memo, useEffect, useMemo, useRef, useState } from 'react'
import { motion } from 'framer-motion'
import type { ITocItem } from './TocItem'

import { RightToLeftTransitionView } from '~/components/ui/transition/RightToLeftTransitionView'
Expand Down Expand Up @@ -80,7 +81,7 @@ export const Toc: Component<TocProps> = ({ useAsWeight, className }) => {
return (
<aside className={clsxm('st-toc z-[3]', 'relative font-sans', className)}>
<ul
className="absolute max-h-[75vh] overflow-y-auto font-medium scrollbar-none"
className="absolute max-h-[75vh] overflow-y-auto px-2 font-medium scrollbar-none"
key={`${toc.map((i) => i.title).join('')}`}
ref={containerRef}
>
Expand Down Expand Up @@ -124,8 +125,15 @@ const MemoedItem = memo<{
}
key={heading.title}
as="li"
className="leading-none"
className="relative leading-none"
>
{isActive && (
<motion.span
layoutId="active-toc-item"
layout
className="absolute -left-2 bottom-[3px] top-[3px] w-[2px] rounded-sm bg-accent"
/>
)}
<TocItem
anchorId={heading.anchorId}
// containerRef={containerRef}
Expand Down
16 changes: 16 additions & 0 deletions src/lib/attach-ua.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { isServer } from '@tanstack/react-query'
import { headers } from 'next/dist/client/components/headers'

import { $axios } from '~/utils/request'

import PKG from '../../package.json'

export const attachUA = () => {
if (!isServer) return
const { get } = headers()

const ua = get('user-agent')
$axios.defaults.headers.common[
'User-Agent'
] = `${ua} NextJS/v${PKG.dependencies.next} ${PKG.name}/${PKG.version}`
}
9 changes: 3 additions & 6 deletions src/lib/define-metadata.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { headers } from 'next/dist/client/components/headers'
import type { AggregateRoot } from '@mx-space/api-client'
import type { Metadata } from 'next'

import { captureException } from '@sentry/nextjs'

import { queries } from '~/queries/definition'
import { getQueryClient } from '~/utils/query-client.server'
import { $axios } from '~/utils/request'

import { attachUA } from './attach-ua'

export const defineMetadata = <T extends Record<string, string>>(
fn: (
Expand All @@ -16,11 +16,8 @@ export const defineMetadata = <T extends Record<string, string>>(
) => {
const handler = async ({ params }: { params: T }): Promise<Metadata> => {
const getData = async () => {
const { get } = headers()
const queryClient = getQueryClient()
const ua = get('user-agent')
$axios.defaults.headers.common['User-Agent'] = ua

attachUA()
return await queryClient.fetchQuery({
...queries.aggregation.root(),
})
Expand Down

1 comment on commit d459308

@vercel
Copy link

@vercel vercel bot commented on d459308 Jun 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

springtide – ./

springtide-git-main-innei.vercel.app
springtide.vercel.app
springtide-innei.vercel.app

Please sign in to comment.