forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* pull changes from docs-playground * cleanup, add callout banner * cleanup linting and test fixes * add discussion link Co-authored-by: James M. Greene <JamesMGreene@github.com>
- Loading branch information
1 parent
94200f3
commit 06d8f81
Showing
37 changed files
with
8,707 additions
and
3,061 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import React, { createContext, useContext, useState } from 'react' | ||
import { CodeLanguage, PlaygroundArticleT } from 'components/playground/types' | ||
import { useRouter } from 'next/router' | ||
|
||
import jsArticle from 'components/playground/content/building-and-testing/nodejs' | ||
import pyArticle from 'components/playground/content/building-and-testing/python' | ||
|
||
const articles = [jsArticle, pyArticle] | ||
const articlesByLangId = articles.reduce((obj, item) => { | ||
obj[item.codeLanguageId] = item | ||
return obj | ||
}, {} as Record<string, PlaygroundArticleT | undefined>) | ||
|
||
const codeLanguages: Array<CodeLanguage> = [ | ||
{ | ||
id: 'nodejs', | ||
label: 'Node.js', | ||
}, | ||
{ | ||
id: 'py', | ||
label: 'Python', | ||
}, | ||
] | ||
|
||
type PlaygroundContextT = { | ||
activeSectionIndex: number | ||
setActiveSectionIndex: (sectionIndex: number) => void | ||
scrollToSection: number | undefined | ||
setScrollToSection: (sectionIndex?: number) => void | ||
codeLanguages: Array<CodeLanguage> | ||
currentLanguage: CodeLanguage | ||
article: PlaygroundArticleT | undefined | ||
} | ||
|
||
export const PlaygroundContext = createContext<PlaygroundContextT | null>(null) | ||
|
||
export const usePlaygroundContext = (): PlaygroundContextT => { | ||
const context = useContext(PlaygroundContext) | ||
|
||
if (!context) { | ||
throw new Error('"usePlaygroundContext" may only be used inside "PlaygroundContext.Provider"') | ||
} | ||
|
||
return context | ||
} | ||
|
||
export const PlaygroundContextProvider = (props: { children: React.ReactNode }) => { | ||
const router = useRouter() | ||
const [activeSectionIndex, setActiveSectionIndex] = useState(0) | ||
const [scrollToSection, setScrollToSection] = useState<number>() | ||
const { langId } = router.query | ||
const currentLanguage = codeLanguages.find(({ id }) => id === langId) || codeLanguages[0] | ||
const availableLanguages = codeLanguages.filter(({ id }) => !!articlesByLangId[id]) | ||
|
||
const article = articlesByLangId[currentLanguage.id] | ||
|
||
const context = { | ||
activeSectionIndex, | ||
setActiveSectionIndex, | ||
scrollToSection, | ||
setScrollToSection, | ||
currentLanguage, | ||
codeLanguages: availableLanguages, | ||
article, | ||
} | ||
|
||
return <PlaygroundContext.Provider value={context}>{props.children}</PlaygroundContext.Provider> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { useTheme } from '@primer/components' | ||
|
||
import { useMediaQuery } from './useMediaQuery' | ||
|
||
type Size = 'small' | 'medium' | 'large' | 'xlarge' | ||
export function useBreakpoint(size: Size) { | ||
const { theme } = useTheme() | ||
return useMediaQuery(`(max-width: ${theme?.sizes[size]})`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { useState, useEffect } from 'react' | ||
|
||
interface IOptions { | ||
/** | ||
* Reset the status after a certain number of milliseconds. This is useful | ||
* for showing a temporary success message. | ||
*/ | ||
successDuration?: number | ||
} | ||
|
||
export default function useCopyClipboard( | ||
text: string, | ||
options?: IOptions | ||
): [boolean, () => Promise<void>] { | ||
const [isCopied, setIsCopied] = useState(false) | ||
const successDuration = options && options.successDuration | ||
|
||
useEffect(() => { | ||
if (isCopied && successDuration) { | ||
const id = setTimeout(() => { | ||
setIsCopied(false) | ||
}, successDuration) | ||
|
||
return () => { | ||
clearTimeout(id) | ||
} | ||
} | ||
}, [isCopied, successDuration]) | ||
|
||
return [ | ||
isCopied, | ||
async () => { | ||
try { | ||
await navigator.clipboard.writeText(text) | ||
setIsCopied(true) | ||
} catch { | ||
setIsCopied(false) | ||
} | ||
}, | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { useState, useEffect } from 'react' | ||
|
||
export function useMediaQuery(query: string) { | ||
const [state, setState] = useState( | ||
typeof window !== 'undefined' ? window.matchMedia(query).matches : false | ||
) | ||
|
||
useEffect(() => { | ||
let mounted = true | ||
const mql = window.matchMedia(query) | ||
const onChange = () => { | ||
if (!mounted) { | ||
return | ||
} | ||
setState(!!mql.matches) | ||
} | ||
|
||
mql.addEventListener('change', onChange) | ||
setState(mql.matches) | ||
|
||
return () => { | ||
mounted = false | ||
mql.removeEventListener('change', onChange) | ||
} | ||
}, [query]) | ||
|
||
return state | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { useState, useEffect } from 'react' | ||
|
||
// returns scroll position | ||
export function useWindowScroll(): number { | ||
const [scrollPosition, setScrollPosition] = useState( | ||
typeof window !== 'undefined' ? window.scrollY : 0 | ||
) | ||
|
||
useEffect(() => { | ||
const setScollPositionCallback = () => setScrollPosition(window.scrollY) | ||
|
||
if (typeof window !== 'undefined') { | ||
window.addEventListener('scroll', setScollPositionCallback) | ||
} | ||
|
||
return () => { | ||
if (typeof window !== 'undefined') { | ||
window.removeEventListener('scroll', setScollPositionCallback) | ||
} | ||
} | ||
}, []) | ||
|
||
return scrollPosition | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const getAnchorLink = (title: string) => title.toLowerCase().replace(/\s/g, '-') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import React from 'react' | ||
import { useTheme } from '@primer/components' | ||
import ReactMarkdown from 'react-markdown' | ||
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter' | ||
import { vs, vscDarkPlus } from 'react-syntax-highlighter/dist/cjs/styles/prism' | ||
import gfm from 'remark-gfm' | ||
|
||
type Props = { | ||
className?: string | ||
children: string | ||
} | ||
export const ArticleMarkdown = ({ className, children }: Props) => { | ||
const theme = useTheme() | ||
|
||
return ( | ||
<ReactMarkdown | ||
className={className} | ||
remarkPlugins={[gfm as any]} | ||
components={{ | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
code: ({ node, inline, className, children, ...props }) => { | ||
const match = /language-(\w+)/.exec(className || '') | ||
return !inline && match ? ( | ||
<SyntaxHighlighter | ||
style={theme.colorScheme === 'dark' ? vscDarkPlus : vs} | ||
language={match[1]} | ||
PreTag="div" | ||
children={String(children).replace(/\n$/, '')} | ||
{...(props as any)} | ||
/> | ||
) : ( | ||
<code className={className} {...props}> | ||
{children} | ||
</code> | ||
) | ||
}, | ||
}} | ||
> | ||
{children} | ||
</ReactMarkdown> | ||
) | ||
} |
Oops, something went wrong.