Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add transitions to Sections bg and line #2491

Merged
merged 4 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions packages/gitbook/src/components/PageAside/AnimatedLine.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use client';

import { Transition, motion, useReducedMotion } from 'framer-motion';
import React from 'react';

import { tcls } from '@/lib/tailwind';

export function AnimatedLine({ transition }: { transition?: Transition }) {
const prefersReducedMotion = useReducedMotion();

return (
<motion.div
layout
layoutId="sections-line"
className={tcls([
'border-primary',
'border-l',
'dark:border-primary-400',
'h-full',
'absolute',
'z-20',
'-left-[5px]',
])}
transition={prefersReducedMotion ? { duration: 0 } : transition}
/>
);
}
37 changes: 26 additions & 11 deletions packages/gitbook/src/components/PageAside/ScrollSectionsList.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
'use client';

import { motion } from 'framer-motion';
import React from 'react';

import { useScrollActiveId } from '@/components/hooks';
import { DocumentSection } from '@/lib/document';
import { tcls } from '@/lib/tailwind';

import { AnimatedLine } from './AnimatedLine';
import { HEADER_HEIGHT_DESKTOP } from '../layout';

/**
* The threshold at which we consider a section as intersecting the viewport.
*/
const SECTION_INTERSECTING_THRESHOLD = 0.9;

const springCurve = {
type: 'spring',
stiffness: 700,
damping: 50,
mass: 0.8,
};

export function ScrollSectionsList(props: { sections: DocumentSection[] }) {
const { sections } = props;

Expand All @@ -28,37 +36,44 @@ export function ScrollSectionsList(props: { sections: DocumentSection[] }) {
});

return (
<ul className={tcls('border-l', 'border-dark/2', 'dark:border-light/1', 'space-y-1')}>
<ul className={tcls('border-l', 'border-dark/2', 'dark:border-light/1', 'pl-1')}>
{sections.map((section) => (
<li key={section.id} className={tcls('flex', 'flex-row')}>
<motion.li
key={section.id}
className={tcls('flex', 'flex-row', 'relative', 'h-fit')}
>
{activeId === section.id ? <AnimatedLine transition={springCurve} /> : null}
<a
href={`#${section.id}`}
className={tcls(
'flex',
'flex-row',
'z-10',
'w-full',
'items-baseline',
'left-[-1px]',
'relative',
'text-sm',
'py-1',
'ps-3',
'hover:text-primary',
'pe-2',
'transition-all',
'border-l',
'border-transparent',
'duration-200',
section.depth > 1 ? ['ps-6', 'opacity-8'] : null,
activeId === section.id
? [
'text-primary',
'border-primary',
'dark:text-primary-400',
'dark:border-primary-400',
'opacity-[1]',
'[&>span]:bg-primary-400',
'dark:[&>span]:bg-primary-600',
'dark:[&>span]:text-dark',
]
: '',
: [
'text-neutral-500',
'dark:text-neutral-400',
'hover:text-neutral-900',
'dark:hover:text-neutral-100',
],
)}
>
{section.tag ? (
Expand All @@ -71,7 +86,7 @@ export function ScrollSectionsList(props: { sections: DocumentSection[] }) {

{section.title}
</a>
</li>
</motion.li>
))}
</ul>
);
Expand Down
Loading