diff options
author | 2021-07-15 14:13:35 -0400 | |
---|---|---|
committer | 2021-07-15 14:13:35 -0400 | |
commit | 166e22bdf35b9751dc42f5cc6cb27a99b077df93 (patch) | |
tree | e8643360c2250dda82ebf95b62281978547d5188 /docs/src/components | |
parent | fb8bf7ec43f7b09348c12d5ff9f81131c70ef5e9 (diff) | |
download | astro-166e22bdf35b9751dc42f5cc6cb27a99b077df93.tar.gz astro-166e22bdf35b9751dc42f5cc6cb27a99b077df93.tar.zst astro-166e22bdf35b9751dc42f5cc6cb27a99b077df93.zip |
merge in docs site (#705)
Diffstat (limited to 'docs/src/components')
-rw-r--r-- | docs/src/components/ArticleFooter.astro | 16 | ||||
-rw-r--r-- | docs/src/components/AstroLogo.astro | 20 | ||||
-rw-r--r-- | docs/src/components/AvatarList.astro | 151 | ||||
-rw-r--r-- | docs/src/components/DocSidebar.tsx | 65 | ||||
-rw-r--r-- | docs/src/components/EditOnGithub.tsx | 25 | ||||
-rw-r--r-- | docs/src/components/MenuToggle.tsx | 44 | ||||
-rw-r--r-- | docs/src/components/Note.astro | 48 | ||||
-rw-r--r-- | docs/src/components/SiteSidebar.astro | 69 | ||||
-rw-r--r-- | docs/src/components/ThemeToggle.tsx | 97 |
9 files changed, 535 insertions, 0 deletions
diff --git a/docs/src/components/ArticleFooter.astro b/docs/src/components/ArticleFooter.astro new file mode 100644 index 000000000..48de51054 --- /dev/null +++ b/docs/src/components/ArticleFooter.astro @@ -0,0 +1,16 @@ +--- +import AvatarList from './AvatarList.astro'; +const { path } = Astro.props; +--- + +<footer> + <AvatarList path={path} /> +</footer> + +<style> +footer { + margin-top: auto; + padding: 2rem 0; + border-top: 3px solid var(--theme-divider); +} +</style> diff --git a/docs/src/components/AstroLogo.astro b/docs/src/components/AstroLogo.astro new file mode 100644 index 000000000..ff1939ad9 --- /dev/null +++ b/docs/src/components/AstroLogo.astro @@ -0,0 +1,20 @@ +--- +const {size} = Astro.props; +--- +<svg class="logo" width={size} height={size} viewBox="0 0 256 256" fill="none" xmlns="http://www.w3.org/2000/svg"> + <style> + #flame { + /* fill: #ff5d01; */ + fill: #3894ff; + } + #a { + /* fill: #000014; */ + fill: #3894ff; + } + </style> + <title>Logo</title> + <path id="a" fill-rule="evenodd" clip-rule="evenodd" + d="M163.008 18.929c1.944 2.413 2.935 5.67 4.917 12.181l43.309 142.27a180.277 180.277 0 00-51.778-17.53l-28.198-95.29a3.67 3.67 0 00-7.042.01l-27.857 95.232a180.225 180.225 0 00-52.01 17.557l43.52-142.281c1.99-6.502 2.983-9.752 4.927-12.16a15.999 15.999 0 016.484-4.798c2.872-1.154 6.271-1.154 13.07-1.154h31.085c6.807 0 10.211 0 13.086 1.157a16.004 16.004 0 016.487 4.806z" /> + <path id="flame" fill-rule="evenodd" clip-rule="evenodd" + d="M168.19 180.151c-7.139 6.105-21.39 10.268-37.804 10.268-20.147 0-37.033-6.272-41.513-14.707-1.602 4.835-1.961 10.367-1.961 13.902 0 0-1.056 17.355 11.015 29.426 0-6.268 5.081-11.349 11.349-11.349 10.743 0 10.731 9.373 10.721 16.977v.679c0 11.542 7.054 21.436 17.086 25.606a23.27 23.27 0 01-2.339-10.2c0-11.008 6.463-15.107 13.974-19.87 5.976-3.79 12.616-8.001 17.192-16.449a31.024 31.024 0 003.743-14.82c0-3.299-.513-6.479-1.463-9.463z" /> +</svg>
\ No newline at end of file diff --git a/docs/src/components/AvatarList.astro b/docs/src/components/AvatarList.astro new file mode 100644 index 000000000..09fc089b9 --- /dev/null +++ b/docs/src/components/AvatarList.astro @@ -0,0 +1,151 @@ +--- +// fetch all commits for just this page's path +const { path } = Astro.props; +const url = `https://api.github.com/repos/snowpackjs/astro-docs/commits?path=${path}`; +const commitsURL = `https://github.com/snowpackjs/astro-docs/commits/main/${path}`; + +async function getCommits(url) { + try { + const token = import.meta.env.SNOWPACK_PUBLIC_GITHUB_TOKEN; + if (!token) { + throw new Error( + 'Cannot find "SNOWPACK_PUBLIC_GITHUB_TOKEN" used for escaping rate-limiting.' + ); + } + + const auth = `Basic ${Buffer.from(token, "binary").toString("base64")}`; + + const res = await fetch(url, { + method: "GET", + headers: { + Authorization: auth, + "User-Agent": "astro-docs/1.0", + }, + }); + + const data = await res.json(); + + if (!res.ok) { + throw new Error( + `Request to fetch commits failed. Reason: ${res.statusText} + Message: ${data.message}` + ); + } + + return data; + } catch (e) { + console.warn(`[error] /src/components/AvatarList.astro + ${e?.message ?? e}`); + return new Array(); + } +} + +function removeDups(arr) { + if (!arr) { + return new Array(); + } + let map = new Map(); + + for (let item of arr) { + let author = item.author; + // Deduplicate based on author.id + map.set(author.id, { login: author.login, id: author.id }); + } + + return Array.from(map.values()); +} + +const data = await getCommits(url); +const unique = removeDups(data); +const recentContributors = unique.slice(0, 3); // only show avatars for the 3 most recent contributors +const additionalContributors = unique.length - recentContributors.length; // list the rest of them as # of extra contributors + +--- +<!-- Thanks to @5t3ph for https://smolcss.dev/#smol-avatar-list! --> +<div class="contributors"> +<ul class="avatar-list" style={`--avatar-count: ${recentContributors.length}`}> + +{recentContributors.map((item) => ( + <li><a href={`https://github.com/${item.login}`}><img alt={`Contributor ${item.login}`} title={`Contributor ${item.login}`} width="64" height="64" src={`https://avatars.githubusercontent.com/u/${item.id}`}/></a></li> + +))} + </ul> + {additionalContributors > 0 && <span><a href={commitsURL}>{`and ${additionalContributors} additional contributor${additionalContributors > 1 ? 's' : ''}.`}</a></span>} + {unique.length === 0 && <a href={commitsURL}>Contributors</a>} +</div> + +<style> +.avatar-list { + --avatar-size: 2.5rem; + --avatar-count: 3; + + display: grid; + list-style: none; + /* Default to displaying most of the avatar to + enable easier access on touch devices, ensuring + the WCAG touch target size is met or exceeded */ + grid-template-columns: repeat( + var(--avatar-count), + max(44px, calc(var(--avatar-size) / 1.15)) + ); + /* `padding` matches added visual dimensions of + the `box-shadow` to help create a more accurate + computed component size */ + padding: 0.08em; + font-size: var(--avatar-size); +} + +@media (any-hover: hover) and (any-pointer: fine) { + .avatar-list { + /* We create 1 extra cell to enable the computed + width to match the final visual width */ + grid-template-columns: repeat( + calc(var(--avatar-count) + 1), + calc(var(--avatar-size) / 1.75) + ); + } +} + +.avatar-list li { + width: var(--avatar-size); + height: var(--avatar-size); +} + +.avatar-list li:hover ~ li a, +.avatar-list li:focus-within ~ li a { + transform: translateX(33%); +} + +.avatar-list img, +.avatar-list a { + display: block; + border-radius: 50%; +} + +.avatar-list a { + transition: transform 180ms ease-in-out; +} + +.avatar-list img { + width: 100%; + height: 100%; + object-fit: cover; + background-color: #fff; + box-shadow: 0 0 0 0.05em #fff, 0 0 0 0.08em rgba(0, 0, 0, 0.15); +} + +.avatar-list a:focus { + outline: 2px solid transparent; + /* Double-layer trick to work for dark and light backgrounds */ + box-shadow: 0 0 0 0.08em var(--theme-accent), 0 0 0 0.12em white; +} + +.contributors { + display: flex; + align-items: center; +} + +.contributors > * + * { + margin-left: .75rem; +} +</style> diff --git a/docs/src/components/DocSidebar.tsx b/docs/src/components/DocSidebar.tsx new file mode 100644 index 000000000..24e9dc4d9 --- /dev/null +++ b/docs/src/components/DocSidebar.tsx @@ -0,0 +1,65 @@ +import type { FunctionalComponent } from 'preact'; +import { h } from 'preact'; +import { useState, useEffect, useRef } from 'preact/hooks'; +import EditOnGithub from './EditOnGithub'; + +const DocSidebar: FunctionalComponent<{ headers: any[]; editHref: string }> = ({ + headers = [], + editHref, +}) => { + const itemOffsets = useRef([]); + const [activeId, setActiveId] = useState<string>(undefined); + + useEffect(() => { + const getItemOffsets = () => { + const titles = document.querySelectorAll('article :is(h1, h2, h3, h4)'); + itemOffsets.current = Array.from(titles).map((title) => ({ + id: title.id, + topOffset: title.getBoundingClientRect().top + window.scrollY, + })); + }; + + getItemOffsets(); + window.addEventListener('resize', getItemOffsets); + + return () => { + window.removeEventListener('resize', getItemOffsets); + }; + }, []); + + return ( + <nav class="sidebar-nav"> + <div class="sidebar-nav-inner"> + <h2 class="heading">On this page</h2> + <ul> + <li + class={`header-link depth-2 ${ + activeId === 'overview' ? 'active' : '' + }`.trim()} + > + <a href="#overview">Overview</a> + </li> + {headers + .filter(({ depth }) => depth > 1 && depth < 4) + .map((header) => ( + <li + class={`header-link depth-${header.depth} ${ + activeId === header.slug ? 'active' : '' + }`.trim()} + > + <a href={`#${header.slug}`}>{header.text}</a> + </li> + ))} + </ul> + <h2 class="heading">More</h2> + <ul> + <li class={`header-link depth-2`}> + <EditOnGithub href={editHref} /> + </li> + </ul> + </div> + </nav> + ); +}; + +export default DocSidebar; diff --git a/docs/src/components/EditOnGithub.tsx b/docs/src/components/EditOnGithub.tsx new file mode 100644 index 000000000..5ff74e364 --- /dev/null +++ b/docs/src/components/EditOnGithub.tsx @@ -0,0 +1,25 @@ +import type { FunctionalComponent } from 'preact'; +import { h } from 'preact'; + +const EditOnGithub: FunctionalComponent<{ href: string }> = ({ href }) => { + return ( + <a class="edit-on-github" href={href}> + <svg + preserveAspectRatio="xMidYMid meet" + height="1em" + width="1em" + fill="currentColor" + xmlns="http://www.w3.org/2000/svg" + viewBox="0 0 438.549 438.549" + stroke="none" + > + <g> + <path d="M409.132 114.573c-19.608-33.596-46.205-60.194-79.798-79.8-33.598-19.607-70.277-29.408-110.063-29.408-39.781 0-76.472 9.804-110.063 29.408-33.596 19.605-60.192 46.204-79.8 79.8C9.803 148.168 0 184.854 0 224.63c0 47.78 13.94 90.745 41.827 128.906 27.884 38.164 63.906 64.572 108.063 79.227 5.14.954 8.945.283 11.419-1.996 2.475-2.282 3.711-5.14 3.711-8.562 0-.571-.049-5.708-.144-15.417a2549.81 2549.81 0 0 1-.144-25.406l-6.567 1.136c-4.187.767-9.469 1.092-15.846 1-6.374-.089-12.991-.757-19.842-1.999-6.854-1.231-13.229-4.086-19.13-8.559-5.898-4.473-10.085-10.328-12.56-17.556l-2.855-6.57c-1.903-4.374-4.899-9.233-8.992-14.559-4.093-5.331-8.232-8.945-12.419-10.848l-1.999-1.431c-1.332-.951-2.568-2.098-3.711-3.429-1.142-1.331-1.997-2.663-2.568-3.997-.572-1.335-.098-2.43 1.427-3.289 1.525-.859 4.281-1.276 8.28-1.276l5.708.853c3.807.763 8.516 3.042 14.133 6.851 5.614 3.806 10.229 8.754 13.846 14.842 4.38 7.806 9.657 13.754 15.846 17.847 6.184 4.093 12.419 6.136 18.699 6.136 6.28 0 11.704-.476 16.274-1.423 4.565-.952 8.848-2.383 12.847-4.285 1.713-12.758 6.377-22.559 13.988-29.41-10.848-1.14-20.601-2.857-29.264-5.14-8.658-2.286-17.605-5.996-26.835-11.14-9.235-5.137-16.896-11.516-22.985-19.126-6.09-7.614-11.088-17.61-14.987-29.979-3.901-12.374-5.852-26.648-5.852-42.826 0-23.035 7.52-42.637 22.557-58.817-7.044-17.318-6.379-36.732 1.997-58.24 5.52-1.715 13.706-.428 24.554 3.853 10.85 4.283 18.794 7.952 23.84 10.994 5.046 3.041 9.089 5.618 12.135 7.708 17.705-4.947 35.976-7.421 54.818-7.421s37.117 2.474 54.823 7.421l10.849-6.849c7.419-4.57 16.18-8.758 26.262-12.565 10.088-3.805 17.802-4.853 23.134-3.138 8.562 21.509 9.325 40.922 2.279 58.24 15.036 16.18 22.559 35.787 22.559 58.817 0 16.178-1.958 30.497-5.853 42.966-3.9 12.471-8.941 22.457-15.125 29.979-6.191 7.521-13.901 13.85-23.131 18.986-9.232 5.14-18.182 8.85-26.84 11.136-8.662 2.286-18.415 4.004-29.263 5.146 9.894 8.562 14.842 22.077 14.842 40.539v60.237c0 3.422 1.19 6.279 3.572 8.562 2.379 2.279 6.136 2.95 11.276 1.995 44.163-14.653 80.185-41.062 108.068-79.226 27.88-38.161 41.825-81.126 41.825-128.906-.01-39.771-9.818-76.454-29.414-110.049z"></path> + </g> + </svg> + <span>Edit on GitHub</span> + </a> + ); +}; + +export default EditOnGithub; diff --git a/docs/src/components/MenuToggle.tsx b/docs/src/components/MenuToggle.tsx new file mode 100644 index 000000000..605581077 --- /dev/null +++ b/docs/src/components/MenuToggle.tsx @@ -0,0 +1,44 @@ +import type { FunctionalComponent } from 'preact'; +import { h, Fragment } from 'preact'; +import { useState, useEffect } from 'preact/hooks'; + +const MenuToggle: FunctionalComponent = () => { + const [sidebarShown, setSidebarShown] = useState(false); + + useEffect(() => { + const body = document.getElementsByTagName('body')[0]; + if (sidebarShown) { + body.classList.add('mobile-sidebar-toggle'); + } else { + body.classList.remove('mobile-sidebar-toggle'); + } + }, [sidebarShown]); + + return ( + <button + type="button" + aria-pressed={sidebarShown ? 'true' : 'false'} + id="menu-toggle" + onClick={() => setSidebarShown(!sidebarShown)} + > + <svg + xmlns="http://www.w3.org/2000/svg" + width="1em" + height="1em" + fill="none" + viewBox="0 0 24 24" + stroke="currentColor" + > + <path + stroke-linecap="round" + stroke-linejoin="round" + stroke-width="2" + d="M4 6h16M4 12h16M4 18h16" + /> + </svg> + <span className="sr-only">Toggle sidebar</span> + </button> + ); +}; + +export default MenuToggle; diff --git a/docs/src/components/Note.astro b/docs/src/components/Note.astro new file mode 100644 index 000000000..c3ae29cb4 --- /dev/null +++ b/docs/src/components/Note.astro @@ -0,0 +1,48 @@ +--- +const { type = 'tip', title } = Astro.props; +--- + +<aside class={`note type-${type}`}> + {title && <label>{title}</label>} + <slot /> +</aside> + +<style> + .note { + --padding-block: 1rem; + --padding-inline: 1.25rem; + + display: flex; + flex-direction: column; + + padding: var(--padding-block) var(--padding-inline); + margin-left: calc(var(--padding-inline) * -1); + margin-right: calc(var(--padding-inline) * -1); + + background: var(--theme-bg-offset); + border-left: calc(var(--padding-inline) / 2) solid var(--color); + border-radius: 0; + } + + .note label { + font-weight: 500; + color: var(--color); + } + + /* .note :global(a) { + color: var(--color); + } */ + + .note.type-tip { + --color: var(--color-green); + --color-rgb: var(--color-green-rgb); + } + .note.type-warning { + --color: var(--color-yellow); + --color-rgb: var(--color-yellow-rgb); + } + .note.type-error { + --color: var(--color-red); + --color-rgb: var(--color-red-rgb); + } +</style> diff --git a/docs/src/components/SiteSidebar.astro b/docs/src/components/SiteSidebar.astro new file mode 100644 index 000000000..6d003d71d --- /dev/null +++ b/docs/src/components/SiteSidebar.astro @@ -0,0 +1,69 @@ +--- +import { sidebar } from '../config.ts'; +const {currentPage} = Astro.props; +--- + + +<nav> + <ul class="nav-groups"> + {sidebar.map(category => ( + <li> + <div class="nav-group"> + <h2 class="nav-group-title">{category.text}</h2> + <ul> + {category.children.map(child => ( + <li class={`nav-link ${currentPage === child.link ? 'is-active' : ''}`}><a href={`${Astro.site.pathname}${child.link}`}>{child.text}</a></li> + ))} + </ul> + </div> + </li> + ))} + </ul> +</nav> + +<style> + nav { + position: sticky; + min-height: calc(100vh - 3.5rem); + height: calc(100vh - 3.5rem); + top: 3.5rem; + } + .nav-groups { + height: 100%; + padding: 2rem 0; + overflow: auto; + } + + .nav-groups > li + li { + margin-top: 2rem; + } + + .nav-group-title { + font-size: 1.0rem; + font-weight: 700; + padding: 0.1rem 2rem; + text-transform: uppercase; + margin-bottom: 0.5rem; + } + + .nav-link a { + font-size: 1.0rem; + margin: 1px; + padding: 0.3rem 2rem; + font: inherit; + color: inherit; + text-decoration: none; + display: block; + } + .nav-link a:hover, + .nav-link a:focus { + background-color: var(--theme-bg-hover); + } + + .nav-link.is-active a { + color: var(--theme-text-accent); + background-color: var(--theme-bg-accent); + font-weight: 600; + } + +</style>
\ No newline at end of file diff --git a/docs/src/components/ThemeToggle.tsx b/docs/src/components/ThemeToggle.tsx new file mode 100644 index 000000000..31ab5ea74 --- /dev/null +++ b/docs/src/components/ThemeToggle.tsx @@ -0,0 +1,97 @@ +import type { FunctionalComponent } from 'preact'; +import { h, Fragment } from 'preact'; +import { useState, useEffect } from 'preact/hooks'; + +const themes = ['system', 'light', 'dark']; + +const icons = [ + <svg + xmlns="http://www.w3.org/2000/svg" + width="20" + height="20" + viewBox="0 0 20 20" + fill="currentColor" + > + <path + fill-rule="evenodd" + d="M3 5a2 2 0 012-2h10a2 2 0 012 2v8a2 2 0 01-2 2h-2.22l.123.489.804.804A1 1 0 0113 18H7a1 1 0 01-.707-1.707l.804-.804L7.22 15H5a2 2 0 01-2-2V5zm5.771 7H5V5h10v7H8.771z" + clip-rule="evenodd" + /> + </svg>, + <svg + xmlns="http://www.w3.org/2000/svg" + width="20" + height="20" + viewBox="0 0 20 20" + fill="currentColor" + > + <path + fillRule="evenodd" + d="M10 2a1 1 0 011 1v1a1 1 0 11-2 0V3a1 1 0 011-1zm4 8a4 4 0 11-8 0 4 4 0 018 0zm-.464 4.95l.707.707a1 1 0 001.414-1.414l-.707-.707a1 1 0 00-1.414 1.414zm2.12-10.607a1 1 0 010 1.414l-.706.707a1 1 0 11-1.414-1.414l.707-.707a1 1 0 011.414 0zM17 11a1 1 0 100-2h-1a1 1 0 100 2h1zm-7 4a1 1 0 011 1v1a1 1 0 11-2 0v-1a1 1 0 011-1zM5.05 6.464A1 1 0 106.465 5.05l-.708-.707a1 1 0 00-1.414 1.414l.707.707zm1.414 8.486l-.707.707a1 1 0 01-1.414-1.414l.707-.707a1 1 0 011.414 1.414zM4 11a1 1 0 100-2H3a1 1 0 000 2h1z" + clipRule="evenodd" + /> + </svg>, + <svg + xmlns="http://www.w3.org/2000/svg" + width="20" + height="20" + viewBox="0 0 20 20" + fill="currentColor" + > + <path d="M17.293 13.293A8 8 0 016.707 2.707a8.001 8.001 0 1010.586 10.586z" /> + </svg>, +]; + +const ThemeToggle: FunctionalComponent = () => { + const [theme, setTheme] = useState(themes[0]); + + useEffect(() => { + const user = localStorage.getItem('theme'); + if (!user) return; + setTheme(user); + }, []); + + useEffect(() => { + const root = document.documentElement; + if (theme === 'system') { + localStorage.removeItem('theme'); + if (window.matchMedia('(prefers-color-scheme: dark)').matches) { + root.classList.add('theme-dark'); + } else { + root.classList.remove('theme-dark'); + } + } else { + localStorage.setItem('theme', theme); + if (theme === 'light') { + root.classList.remove('theme-dark'); + } else { + root.classList.add('theme-dark'); + } + } + }, [theme]); + + return ( + <div id="theme-toggle"> + {themes.map((t, i) => { + const icon = icons[i]; + const checked = t === theme; + return ( + <label className={checked ? 'checked' : ''}> + {icon} + <input + type="radio" + name="theme-toggle" + checked={checked} + value={t} + title={`Use ${t} theme`} + aria-label={`Use ${t} theme`} + onChange={() => setTheme(t)} + /> + </label> + ); + })} + </div> + ); +}; + +export default ThemeToggle; |