Skip to content

Commit 1f0ad83

Browse files
committed
Add anchor component
1 parent 95d0b3f commit 1f0ad83

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

packages/scrollycoding/src/focus.tsx

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@ export interface FocusProps {
1313
file?: string
1414
}
1515

16-
export function Focus({ children, ...props }: FocusProps) {
16+
export function Focus({
17+
children,
18+
...props
19+
}: FocusProps): JSX.Element {
1720
const hikeContext = useHikeContext()
1821
return hikeContext.layout === "fixed" ? (
19-
children
22+
(children as JSX.Element)
2023
) : (
2124
<FocusButton
2225
context={hikeContext}
@@ -91,3 +94,31 @@ function Icon({ isFocused }: { isFocused: boolean }) {
9194
</svg>
9295
)
9396
}
97+
98+
export function withFocusHandler(type: any = "a") {
99+
function AnchorWithFocus(props: any) {
100+
return <AnchorOrFocus type={type} {...props} />
101+
}
102+
return AnchorWithFocus
103+
}
104+
105+
export function AnchorOrFocus({
106+
type = "a",
107+
href,
108+
...props
109+
}: any) {
110+
if (!href.startsWith("focus://")) {
111+
return React.createElement(type, { href, ...props })
112+
}
113+
114+
const [firstPart, secondPart] = decodeURI(href)
115+
.substr("focus://".length)
116+
.split("#")
117+
const hasFile = Boolean(secondPart)
118+
119+
return hasFile ? (
120+
<Focus on={secondPart} file={firstPart} {...props} />
121+
) : (
122+
<Focus on={firstPart} {...props} />
123+
)
124+
}

packages/scrollycoding/src/index.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import * as React from "react"
22
import "./index.scss"
33
import { Hike } from "./hike"
44

5-
import { Focus } from "./focus"
5+
import {
6+
Focus,
7+
AnchorOrFocus,
8+
withFocusHandler,
9+
} from "./focus"
610
import { CodeSlot, PreviewSlot } from "./fixed-layout"
711
import { useMdxSteps, StepHead } from "./mdx-steps"
812
import {
@@ -21,4 +25,6 @@ export {
2125
PreviewProps,
2226
useMdxSteps,
2327
StepHead,
28+
AnchorOrFocus,
29+
withFocusHandler,
2430
}

packages/scrollycoding/src/mdx-steps.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ export interface StepHeadProps {
1616
previewProps?: Partial<PreviewProps>
1717
}
1818

19-
function StepHead(props: StepHeadProps) {
20-
return null
19+
function StepHead(props: StepHeadProps): JSX.Element {
20+
return (null as any) as JSX.Element
2121
}
2222

2323
interface Step {

0 commit comments

Comments
 (0)