forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlatformPicker.tsx
181 lines (161 loc) · 5.64 KB
/
PlatformPicker.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import { useEffect, useState } from 'react'
import Cookies from 'js-cookie'
import { SubNav, TabNav, UnderlineNav } from '@primer/react'
import { sendEvent, EventType } from 'components/lib/events'
import { useRouter } from 'next/router'
import { useArticleContext } from 'components/context/ArticleContext'
import parseUserAgent from 'components/lib/user-agent'
const platforms = [
{ id: 'mac', label: 'Mac' },
{ id: 'windows', label: 'Windows' },
{ id: 'linux', label: 'Linux' },
]
// Nota bene: platform === os
// Imperatively modify article content to show only the selected platform
// find all platform-specific *block* elements and hide or show as appropriate
// example: {% mac %} block content {% endmac %}
function showPlatformSpecificContent(platform: string) {
const markdowns = Array.from(document.querySelectorAll<HTMLElement>('.extended-markdown'))
markdowns
.filter((el) => platforms.some((platform) => el.classList.contains(platform.id)))
.forEach((el) => {
el.style.display = el.classList.contains(platform) ? '' : 'none'
})
// find all platform-specific *inline* elements and hide or show as appropriate
// example: <span class="platform-mac">inline content</span>
const platformEls = Array.from(
document.querySelectorAll<HTMLElement>(
platforms.map((platform) => `.platform-${platform.id}`).join(', ')
)
)
platformEls.forEach((el) => {
el.style.display = el.classList.contains(`platform-${platform}`) ? '' : 'none'
})
}
// uses the order of the supportedPlatforms array to
// determine the default platform
const getFallbackPlatform = (detectedPlatforms: Array<string>): string => {
const foundPlatform = platforms.find((platform) => detectedPlatforms.includes(platform.id))
return foundPlatform?.id || 'linux'
}
type Props = {
variant?: 'subnav' | 'tabnav' | 'underlinenav'
}
export const PlatformPicker = ({ variant = 'subnav' }: Props) => {
const { defaultPlatform, detectedPlatforms } = useArticleContext()
const [currentPlatform, setCurrentPlatform] = useState(defaultPlatform || '')
const { asPath } = useRouter()
// Run on mount for client-side only features
useEffect(() => {
let userAgent = parseUserAgent().os
if (userAgent === 'ios') {
userAgent = 'mac'
}
const platform = defaultPlatform || Cookies.get('osPreferred') || userAgent || 'linux'
setCurrentPlatform(platform)
// always trigger this on initial render. if the default doesn't change the other useEffect won't fire
showPlatformSpecificContent(platform)
}, [asPath])
// Make sure we've always selected a platform that exists in the article
useEffect(() => {
// Only check *after* current platform has been determined
if (currentPlatform && !detectedPlatforms.includes(currentPlatform)) {
setCurrentPlatform(getFallbackPlatform(detectedPlatforms))
}
}, [currentPlatform, detectedPlatforms.join(',')])
const onClickPlatform = (platform: string) => {
setCurrentPlatform(platform)
// imperatively modify the article content
showPlatformSpecificContent(platform)
sendEvent({
type: EventType.preference,
preference_name: 'os',
preference_value: platform,
})
Cookies.set('osPreferred', platform, {
sameSite: 'strict',
secure: true,
})
}
// only show platforms that are in the current article
const platformOptions = platforms.filter((platform) => detectedPlatforms.includes(platform.id))
const sharedContainerProps = {
'data-testid': 'platform-picker',
'aria-label': 'Platform picker',
'data-default-platform': defaultPlatform,
className: 'mb-4',
}
if (variant === 'subnav') {
return (
<SubNav {...sharedContainerProps}>
<SubNav.Links>
{platformOptions.map((option) => {
return (
<SubNav.Link
key={option.id}
data-platform={option.id}
as="button"
selected={option.id === currentPlatform}
onClick={() => {
onClickPlatform(option.id)
}}
>
{option.label}
</SubNav.Link>
)
})}
</SubNav.Links>
</SubNav>
)
}
if (variant === 'underlinenav') {
return (
<UnderlineNav {...sharedContainerProps}>
{platformOptions.map((option) => {
return (
<UnderlineNav.Link
key={option.id}
data-platform={option.id}
as="button"
selected={option.id === currentPlatform}
// Temporary fix: This should be removed when this merges: PR 24123
sx={{
color: 'var(--color-fg-default)',
'&.selected': { color: 'var(--color-fg-default)' },
':hover': { color: 'var(--color-fg-default)' },
':focus': {
color: 'var(--color-fg-default)',
outline: '-webkit-focus-ring-color auto 1px;',
},
}}
onClick={() => {
onClickPlatform(option.id)
}}
>
{option.label}
</UnderlineNav.Link>
)
})}
</UnderlineNav>
)
}
return (
<TabNav {...sharedContainerProps}>
{platformOptions.map((option) => {
return (
<TabNav.Link
key={option.id}
data-platform={option.id}
as="button"
selected={option.id === currentPlatform}
onClick={() => {
onClickPlatform(option.id)
}}
>
{option.label}
</TabNav.Link>
)
})}
</TabNav>
)
}