Skip to content

Add trigger position config #360

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

Merged
merged 2 commits into from
Apr 30, 2023
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
5 changes: 4 additions & 1 deletion packages/mdx/src/mdx-client/scrollycoding.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,10 @@ function DynamicScrollycoding({
style={style}
>
<div className="ch-scrollycoding-content">
<Scroller onStepChange={onStepChange}>
<Scroller
onStepChange={onStepChange}
triggerPosition={codeConfig?.triggerPosition}
>
{stepsChildren.map((children, i) => (
<ScrollerStep
as="div"
Expand Down
64 changes: 45 additions & 19 deletions packages/mdx/src/scroller/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ const useLayoutEffect =
type ScrollerProps = {
onStepChange: (stepIndex: number) => void
children: React.ReactNode
getRootMargin?: (vh: number) => string
getRootMargin?: (
vh: number,
triggerPosition?: TriggerPosition
) => string
triggerPosition?: TriggerPosition
debug?: boolean
}

Expand All @@ -28,30 +32,31 @@ function Scroller({
onStepChange,
children,
getRootMargin = defaultRootMargin,
triggerPosition,
debug = false,
}: ScrollerProps) {
const [
observer,
setObserver,
] = React.useState<IntersectionObserver>()
const [observer, setObserver] =
React.useState<IntersectionObserver>()
const vh = useWindowHeight()

useLayoutEffect(() => {
const windowHeight = vh || 0
const handleIntersect: IntersectionObserverCallback = entries => {
if (debug || (window as any).chDebugScroller) {
debugEntries(entries)
}
entries.forEach(entry => {
if (entry.intersectionRatio > 0) {
const stepElement = (entry.target as unknown) as StepElement
onStepChange(+stepElement.stepIndex)
const handleIntersect: IntersectionObserverCallback =
entries => {
if (debug || (window as any).chDebugScroller) {
debugEntries(entries)
}
})
}
entries.forEach(entry => {
if (entry.intersectionRatio > 0) {
const stepElement =
entry.target as unknown as StepElement
onStepChange(+stepElement.stepIndex)
}
})
}
const observer = newIntersectionObserver(
handleIntersect,
getRootMargin(windowHeight)
getRootMargin(windowHeight, triggerPosition)
)
setObserver(observer)

Expand Down Expand Up @@ -85,7 +90,8 @@ function Step({
}, [observer])

useLayoutEffect(() => {
const stepElement = (ref.current as unknown) as StepElement
const stepElement =
ref.current as unknown as StepElement
stepElement.stepIndex = index
}, [index])

Expand All @@ -103,6 +109,26 @@ function newIntersectionObserver(
})
}

function defaultRootMargin(vh: number) {
return `-${vh / 2 - 2}px 0px`
type TriggerPosition = `${number}px` | `${number}%`

function defaultRootMargin(
vh: number,
triggerPosition = "50%"
) {
let y = vh * 0.5

if (triggerPosition.endsWith("%")) {
const percent = parseFloat(
triggerPosition.replace("%", "")
)
y = vh * (percent / 100)
} else if (triggerPosition.endsWith("px")) {
y = parseFloat(triggerPosition.replace("px", ""))
}

if (y < 0) {
y = vh + y
}

return `-${y - 2}px 0px -${vh - y - 2}px`
}
3 changes: 3 additions & 0 deletions packages/mdx/src/smooth-code/code-tween.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ type HTMLProps = React.DetailedHTMLProps<
HTMLDivElement
>

type TriggerPosition = `${number}px` | `${number}%`

export type CodeTweenProps = {
tween: FullTween<CodeStep>
progress: number
Expand All @@ -49,6 +51,7 @@ export type CodeConfig = {
showExpandButton?: boolean
staticMediaQuery?: string
rows?: number | "focus" | (number | "focus")[]
triggerPosition?: TriggerPosition
debug?: boolean
}

Expand Down