-
Notifications
You must be signed in to change notification settings - Fork 48
/
index.tsx
140 lines (105 loc) · 3.73 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { ComponentPropsWithoutRef, forwardRef, MouseEvent } from 'react'
import scrollIntoView from 'scroll-into-view-if-needed'
import NextLink from 'next/link'
import { LinkValue as LinkPropControllerValue } from '../../../prop-controllers/descriptors'
import { usePage } from '../../hooks'
import { useElementId } from '../../../runtimes/react'
type BaseProps = {
link?: LinkPropControllerValue
onClick?: (event: MouseEvent<HTMLAnchorElement>) => unknown
}
type Props = BaseProps & Omit<ComponentPropsWithoutRef<'a'>, keyof BaseProps>
export const Link = forwardRef<HTMLAnchorElement, Props>(function Link(
{ link, onClick = () => {}, ...restOfProps }: Props,
ref,
) {
const page = usePage(link && link.type === 'OPEN_PAGE' ? link.payload.pageId : null)
const elementKey =
link?.type === 'SCROLL_TO_ELEMENT' ? link.payload.elementIdConfig?.elementKey : null
const elementId = useElementId(elementKey)
// We don't want to use `next/link` with relative paths because Next.js will attempt to normalize
// it and mess up the path.
let useNextLink: boolean | undefined
let href: string | undefined
let target: '_blank' | '_self' | undefined
let block: 'start' | 'center' | 'end' | undefined
if (link) {
switch (link.type) {
case 'OPEN_PAGE': {
if (page) {
useNextLink = true
href = `/${page.pathname}`
}
target = link.payload.openInNewTab ? '_blank' : '_self'
break
}
case 'OPEN_URL': {
useNextLink = true
href = link.payload.url
target = link.payload.openInNewTab ? '_blank' : '_self'
break
}
case 'SEND_EMAIL': {
useNextLink = false
const { to, subject = '', body = '' } = link.payload
if (to != null) href = `mailto:${to}?subject=${subject}&body=${body}`
break
}
case 'CALL_PHONE': {
useNextLink = false
href = `tel:${link.payload.phoneNumber}`
break
}
case 'SCROLL_TO_ELEMENT': {
useNextLink = false
href = `#${elementId ?? ''}`
block = link.payload.block
break
}
default:
throw new RangeError(`Invalid link type "${(link as any).type}."`)
}
}
function handleClick(event: MouseEvent<HTMLAnchorElement>) {
onClick(event)
if (event.defaultPrevented) return
/**
* When we introduced `next/link` instead of just `a` element slate no longer prevented link from navigating within
* content mode. This is a hack to compensate for what would be expected as slate's default behavior.
* On upgrade of slate this can be reevaluated.
*/
if (event.currentTarget.isContentEditable) return event.preventDefault()
if (link && link.type === 'SCROLL_TO_ELEMENT') {
let hash: string | undefined
try {
if (href != null) hash = new URL(`http://www.example.com/${href}`).hash
} catch (error) {
console.error(`Link received invalid href: ${href}`, error)
}
if (href != null && hash != null && href === hash) {
event.preventDefault()
const view = event.view as unknown as Window
scrollIntoView(view.document.querySelector(hash)!, {
behavior: 'smooth',
block,
})
if (view.location.hash !== hash) view.history.pushState({}, '', hash)
}
}
}
if (useNextLink && href != null) {
return (
<NextLink
{...restOfProps}
ref={ref}
target={target}
onClick={handleClick}
href={href}
// Next.js v12 has legacyBehavior set to true by default
legacyBehavior={false}
/>
)
}
// eslint-disable-next-line
return <a {...restOfProps} ref={ref} href={href} target={target} onClick={handleClick} />
})