From 5247a23cbe534d79cdf4abe4c200a351dddc5dc2 Mon Sep 17 00:00:00 2001 From: Nate Moore Date: Thu, 27 May 2021 09:16:14 -0500 Subject: [PATCH] Example: Docs template (#226) * fix: markdown issues * wip: add docs example * example: update doc template * chore: credit Steph for AvatarList * chore: align footer to bottom viewport * chore: feeback R1 * fix: font fallback in firefox * fix merge conflicts * fix: add default value to headers * chore: fix doc example --- .gitignore | 3 + .../src/pages/collections.astro | 211 ----------- examples/astro-markdown/src/pages/index.astro | 2 +- examples/doc/astro.config.mjs | 5 + examples/doc/package.json | 17 + examples/doc/public/code.css | 155 ++++++++ examples/doc/public/favicon.svg | 11 + examples/doc/public/index.css | 350 ++++++++++++++++++ examples/doc/public/theme.css | 73 ++++ examples/doc/public/theme.js | 8 + .../doc/src/components/ArticleFooter.astro | 15 + examples/doc/src/components/AvatarList.astro | 74 ++++ examples/doc/src/components/DocSidebar.tsx | 55 +++ examples/doc/src/components/EditOnGithub.tsx | 13 + examples/doc/src/components/Note.astro | 49 +++ examples/doc/src/components/SiteSidebar.astro | 20 + examples/doc/src/components/ThemeToggle.tsx | 65 ++++ examples/doc/src/config.ts | 9 + examples/doc/src/layouts/Main.astro | 228 ++++++++++++ examples/doc/src/pages/example.md | 35 ++ examples/doc/src/pages/index.astro | 14 + packages/astro/components/Prism.astro | 15 +- 22 files changed, 1209 insertions(+), 218 deletions(-) delete mode 100644 examples/astro-markdown/src/pages/collections.astro create mode 100644 examples/doc/astro.config.mjs create mode 100644 examples/doc/package.json create mode 100644 examples/doc/public/code.css create mode 100644 examples/doc/public/favicon.svg create mode 100644 examples/doc/public/index.css create mode 100644 examples/doc/public/theme.css create mode 100644 examples/doc/public/theme.js create mode 100644 examples/doc/src/components/ArticleFooter.astro create mode 100644 examples/doc/src/components/AvatarList.astro create mode 100644 examples/doc/src/components/DocSidebar.tsx create mode 100644 examples/doc/src/components/EditOnGithub.tsx create mode 100644 examples/doc/src/components/Note.astro create mode 100644 examples/doc/src/components/SiteSidebar.astro create mode 100644 examples/doc/src/components/ThemeToggle.tsx create mode 100644 examples/doc/src/config.ts create mode 100644 examples/doc/src/layouts/Main.astro create mode 100644 examples/doc/src/pages/example.md create mode 100644 examples/doc/src/pages/index.astro diff --git a/.gitignore b/.gitignore index 45421092e..919cc45da 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,6 @@ dist/ _site/ *.log package-lock.json + +# .vscode files other than at top-level +**/.vscode diff --git a/examples/astro-markdown/src/pages/collections.astro b/examples/astro-markdown/src/pages/collections.astro deleted file mode 100644 index efdf85d5e..000000000 --- a/examples/astro-markdown/src/pages/collections.astro +++ /dev/null @@ -1,211 +0,0 @@ ---- -import Markdown from 'astro/components/Markdown.astro'; ---- - - - - Collections - - -
- - # 🍱 Collections - - ## ❓ What are Collections? - - [Fetching data is easy in Astro][docs-data]. But what if you wanted to make a paginated blog? What if you wanted an easy way to sort data, or filter data based on part of the URL? Or generate an RSS 2.0 feed? When you need something a little more powerful than simple data fetching, Astro’s Collections API may be what you need. - - An Astro Collection is similar to the general concept of Collections in static site generators like Jekyll, Hugo, Eleventy, etc. It’s a general way to load an entire data set. But one big difference between Astro Collections and traditional static site generators is: **Astro lets you seamlessly blend remote API data and local files in a JAMstack-friendly way.** To see how, this guide will walk through a few examples. If you’d like, you can reference the [blog example project][example-blog] to see the finished code in context. - - ## 🧑‍🎨 How to Use - - By default, any Astro component can fetch data from any API or local `*.md` files. But what if you had a blog you wanted to paginate? What if you wanted to generate dynamic URLs based on metadata (e.g. `/tag/:tag/`)? Or do both together? Astro Collections are a way to do all of that. It’s perfect for generating blog-like content, or scaffolding out dynamic URLs from your data. - - Let’s pretend we have some blog posts written already. This is our starting project structure: - - ``` - └── src/ - └── pages/ - └── post/ - └── (blog content) - ``` - - The first step in adding some dynamic collections is deciding on a URL schema. For our example website, we’re aiming for the following URLs: - - - `/post/:post`: A single blog post page - - `/posts/:page`: A list page of all blog posts, paginated, and sorted most recent first - - `/tag/:tag`: All blog posts, filtered by a specific tag - - Because `/post/:post` references the static files we have already, that doesn’t need to be a collection. But we will need collections for `/posts/:page` and `/tag/:tag` because those will be dynamically generated. For both collections we’ll create a `/src/pages/$[collection].astro` file. This is our new structure: - - ```diff - └── src/ - └── pages/ - ├── post/ - │ └── (blog content) - + ├── $posts.astro -> /posts/1, /posts/2, … - + └── $tag.astro -> /tag/:tag/1, /tag/:tag/2, … - ``` - - 💁‍ **Tip**: Any `.astro` filename beginning with a `$` is how it’s marked as a collection. - - In each `$[collection].astro` file, we’ll need 2 things: - - ```js - // 1. We need to mark “collection” as a prop (this is a special reserved name) - export let collection: any; - - // 2. We need to export an async createCollection() function that will retrieve our data. - export async function createCollection() { - return { - async data() { - // return data here to load (we’ll cover how later) - }, - }; - } - ``` - - These are important so your data is exposed to the page as a prop, and also Astro has everything it needs to gather your data and generate the proper routes. How it does this is more clear if we walk through a practical example. - - #### Example 1: Simple pagination - - Our blog posts all contain `title`, `tags`, and `published_at` in their frontmatter: - - ```md - --- - title: My Blog Post - tags: - - javascript - published_at: 2021-03-01 09:34:00 - --- - - # My Blog post - - … - ``` - - There’s nothing special or reserved about any of these names; you’re free to name everything whatever you’d like, or have as much or little frontmatter as you need. - - ```jsx - // /src/pages/$posts.astro - --- - export let collection: any; - - export async function createCollection() { - const allPosts = Astro.fetchContent('./post/*.md'); // load data that already lives at `/post/:slug` - allPosts.sort((a, b) => new Date(b.published_at) - new Date(a.published_at)); // sort newest -> oldest (we got "published_at" from frontmatter!) - - // (load more data here, if needed) - - return { - async data() { - return allPosts; - }, - pageSize: 10, // how many we want to show per-page (default: 25) - }; - } - - function formatDate(date) { - return new Date(date).toUTCString(); - } - --- - - - - Blog Posts: page {collection.page.current} - - - - - -
-
Results {collection.start + 1}–{collection.end + 1} of {collection.total}
- {collection.data.map((post) => ( -

{post.title}

- - Read - )} -
-
-

Page {collection.page.current} / {collection.page.last}

- -
- - - ``` - - Let’s walk through some of the key parts: - - - `export let collection`: this is important because it exposes a prop to the page for Astro to return with all your data loaded. ⚠️ **It must be named `collection`**. - - `export async function createCollection()`: this is also required, **and must be named this exactly.** This is an async function that lets you load data from anywhere (even a remote API!). At the end, you must return an object with `{ data: yourData }`. There are other options such as `pageSize` we’ll cover later. - - `{collection.data.map((post) => (…`: this lets us iterate over all the markdown posts. This will take the shape of whatever you loaded in `createCollection()`. It will always be an array. - - `{collection.page.current}`: this, and other properties, simply return more info such as what page a user is on, what the URL is, etc. etc. - - Curious about everything on `collection`? See the [reference][collection-api]. - - #### Example 2: Advanced filtering & pagination - - In our earlier example, we covered simple pagination for `/posts/1`, but we’d still like to make `/tag/:tag/1` and `/year/:year/1`. To do that, we’ll create 2 more collections: `/src/pages/$tag.astro` and `src/pages/$year.astro`. Assume that the markup is the same, but we’ve expanded the `createCollection()` function with more data. - - ```diff - // /src/pages/$tag.astro - --- - import Pagination from '../components/Pagination.astro'; - import PostPreview from '../components/PostPreview.astro'; - - export let collection: any; - - export async function createCollection() { - const allPosts = Astro.fetchContent('./post/*.md'); - allPosts.sort((a, b) => new Date(b.published_at) - new Date(a.published_at)); - + const allTags = [...new Set(allPosts.map((post) => post.tags).flat())]; // gather all unique tags (we got "tags" from frontmatter!) - + allTags.sort((a, b) => a.localeCompare(b)); // sort tags A -> Z - + const routes = allTags.map((tag) => ({ tag })); // this is where we set { params: { tag } } - - return { - - async data() { - - return allPosts; - + async data({ params }) { - + return allPosts.filter((post) => post.tags.includes(params.tag)); // filter posts that match the :tag from the URL ("params") - }, - pageSize: 10, - + routes, - + permalink: ({ params }) => `/tag/${params.tag}/` // this is where we generate our URL structure - }; - } - --- - ``` - - Some important concepts here: - - - `routes = allTags.map((tag) => ({ tag }))`: Astro handles pagination for you automatically. But when it needs to generate multiple routes, this is where you tell Astro about all the possible routes. This way, when you run `astro build`, your static build isn’t missing any pages. - - `` permalink: ({ params }) => `/tag/${params.tag}/` ``: this is where you tell Astro what the generated URL should be. Note that while you have control over this, the root of this must match the filename (it's best **NOT** to use `/pages/$tag.astro` to generate `/year/$year.astro`; that should live at `/pages/$year.astro` as a separate file). - - `allPosts.filter((post) => post.tag === params.tag)`: we aren’t returning all posts here; we’re only returning posts with a matching tag. _What tag,_ you ask? The `routes` array has `[{ tag: 'javascript' }, { tag: '…`, and all the routes we need to gather. So we first need to query everything, but only return the `.filter()`ed posts at the very end. - - Other things of note is that we are sorting like before, but we filter by the frontmatter `tag` property, and return those at URLs. - - These are still paginated, too! But since there are other conditions applied, they live at a different URL. - - #### Tips - - - Having to load different collections in different `$[collection].astro` files might seem like a pain at first, until you remember **you can create reusable components!** Treat `/pages/*.astro` files as your one-off routing & data fetching logic, and treat `/components/*.astro` as your reusable markup. If you find yourself duplicating things too much, you can probably use a component instead! - - Stay true to `/pages/$[collection].astro` naming. If you have an `/all-posts/*` route, then use `/pages/$all-posts.astro` to manage that. Don’t try and trick `permalink` to generate too many URL trees; it’ll only result in pages being missed when it comes time to build. - - ### 📚 Further Reading - - - [Fetching data in Astro][docs-data] - - API Reference: [collection][collection-api] - - API Reference: [createCollection()][create-collection-api] - - API Reference: [Creating an RSS feed][create-collection-api] - - [docs-data]: ../README.md#-fetching-data - [collection-api]: ./api.md#collection - [create-collection-api]: ./api.md#createcollection - [example-blog]: ../examples/blog - [fetch-content]: ./api.md#fetchcontent -
-
- - diff --git a/examples/astro-markdown/src/pages/index.astro b/examples/astro-markdown/src/pages/index.astro index cbff28b5c..e05db0155 100644 --- a/examples/astro-markdown/src/pages/index.astro +++ b/examples/astro-markdown/src/pages/index.astro @@ -17,7 +17,7 @@ const items = ['A', 'B', 'C']; **Astro Markdown** brings native Markdown support to HTML! - > It's inspired by [`mdx`](https://mdxjs.com/) and powered by [`remark`](https://github.com/remarkjs/remark)). + > It's inspired by [`MDX`](https://mdxjs.com/) and powered by [`remark`](https://github.com/remarkjs/remark). The best part? It comes with all the Astro features you expect. diff --git a/examples/doc/astro.config.mjs b/examples/doc/astro.config.mjs new file mode 100644 index 000000000..9ba6c58c9 --- /dev/null +++ b/examples/doc/astro.config.mjs @@ -0,0 +1,5 @@ +export default { + extensions: { + '.tsx': 'preact' + } +}; diff --git a/examples/doc/package.json b/examples/doc/package.json new file mode 100644 index 000000000..e6dc1a8f5 --- /dev/null +++ b/examples/doc/package.json @@ -0,0 +1,17 @@ +{ + "name": "@example/doc", + "version": "0.0.1", + "private": true, + "scripts": { + "start": "astro dev", + "build": "astro build", + "astro-dev": "nodemon --delay 0.5 -w ../../packages/astro/dist -x '../../packages/astro/astro.mjs dev'" + }, + "devDependencies": { + "astro": "^0.11.0", + "nodemon": "^2.0.7" + }, + "snowpack": { + "workspaceRoot": "../.." + } +} diff --git a/examples/doc/public/code.css b/examples/doc/public/code.css new file mode 100644 index 000000000..54b2c5094 --- /dev/null +++ b/examples/doc/public/code.css @@ -0,0 +1,155 @@ +.language-css > code, +.language-sass > code, +.language-scss > code { + color: #fd9170; +} + +[class*="language-"] .namespace { + opacity: 0.7; +} + +.token.atrule { + color: #c792ea; +} + +.token.attr-name { + color: #ffcb6b; +} + +.token.attr-value { + color: #a5e844; +} + +.token.attribute { + color: #a5e844; +} + +.token.boolean { + color: #c792ea; +} + +.token.builtin { + color: #ffcb6b; +} + +.token.cdata { + color: #80cbc4; +} + +.token.char { + color: #80cbc4; +} + +.token.class { + color: #ffcb6b; +} + +.token.class-name { + color: #f2ff00; +} + +.token.comment { + color: #616161; +} + +.token.constant { + color: #c792ea; +} + +.token.deleted { + color: #ff6666; +} + +.token.doctype { + color: #616161; +} + +.token.entity { + color: #ff6666; +} + +.token.function { + color: #c792ea; +} + +.token.hexcode { + color: #f2ff00; +} + +.token.id { + color: #c792ea; + font-weight: bold; +} + +.token.important { + color: #c792ea; + font-weight: bold; +} + +.token.inserted { + color: #80cbc4; +} + +.token.keyword { + color: #c792ea; +} + +.token.number { + color: #fd9170; +} + +.token.operator { + color: #89ddff; +} + +.token.prolog { + color: #616161; +} + +.token.property { + color: #80cbc4; +} + +.token.pseudo-class { + color: #a5e844; +} + +.token.pseudo-element { + color: #a5e844; +} + +.token.punctuation { + color: #89ddff; +} + +.token.regex { + color: #f2ff00; +} + +.token.selector { + color: #ff6666; +} + +.token.string { + color: #a5e844; +} + +.token.symbol { + color: #c792ea; +} + +.token.tag { + color: #ff6666; +} + +.token.unit { + color: #fd9170; +} + +.token.url { + color: #ff6666; +} + +.token.variable { + color: #ff6666; +} diff --git a/examples/doc/public/favicon.svg b/examples/doc/public/favicon.svg new file mode 100644 index 000000000..542f90aec --- /dev/null +++ b/examples/doc/public/favicon.svg @@ -0,0 +1,11 @@ + + + + + diff --git a/examples/doc/public/index.css b/examples/doc/public/index.css new file mode 100644 index 000000000..c5ae87f5c --- /dev/null +++ b/examples/doc/public/index.css @@ -0,0 +1,350 @@ +@import './theme'; +@import './code'; + +* { + box-sizing: border-box; + margin: 0; +} + +:root { + --user-font-scale: 1rem - 16px; + --max-width: calc(100% - 2rem); +} + +@media (min-width: 50em) { + :root { + --max-width: 48em; + } +} + +body { + display: flex; + flex-direction: column; + min-height: 100vh; + font-family: var(--font-body); + font-size: 1rem; + font-size: clamp(0.875rem, 0.4626rem + 1.0309vw + var(--user-font-scale), 1.125rem); + line-height: 1.625; +} + +nav ul { + list-style: none; + padding: 0; +} + +.content main > * + * { + margin-top: 1rem; +} + +/* Typography */ +:is(h1, h2, h3, h4, h5, h6) { + margin-bottom: 1.38rem; + font-weight: 400; + line-height: 1.3; +} + +:is(h1, h2) { + max-width: 40ch; +} + +:is(h2, h3):not(:first-child) { + margin-top: 3rem; +} + +h1 { + font-size: clamp(2.488rem, 1.9240rem + 1.4100vw, 3.052rem); +} + +h2 { + font-size: clamp(2.074rem, 1.7070rem + 0.9175vw, 2.441rem); +} + +h3 { + font-size: clamp(1.728rem, 1.5030rem + 0.5625vw, 1.953rem); +} + +h4 { + font-size: clamp(1.44rem, 1.3170rem + 0.3075vw, 1.563rem); +} + +h5 { + font-size: clamp(1.2rem, 1.1500rem + 0.1250vw, 1.25rem); +} + +p { + color: var(--theme-text-light); +} + +small, .text_small { + font-size: 0.833rem; +} + +a { + color: var(--theme-accent); + font-weight: 400; + text-underline-offset: 0.08em; + text-decoration: none; + display: inline-flex; + align-items: center; + gap: 0.5rem; +} + +a > code:not([class*="language"]) { + position: relative; + color: var(--theme-accent); + background: transparent; + text-underline-offset: var(--padding-block); +} + +a > code:not([class*="language"])::before { + content: ''; + position: absolute; + top: 0; + right: 0; + bottom: 0; + left: 0; + display: block; + background: var(--theme-accent); + opacity: var(--theme-accent-opacity); + border-radius: var(--border-radius); +} + +a:hover, +a:focus { + text-decoration: underline; +} + +a:focus { + outline: 2px solid currentColor; + outline-offset: 0.25em; +} + +strong { + font-weight: 600; + color: inherit; +} + +/* Supporting Content */ + +code:not([class*="language"]) { + --border-radius: 3px; + --padding-block: 0.2rem; + --padding-inline: 0.33rem; + + font-family: var(--font-mono); + font-size: .85em; + color: inherit; + background-color: var(--theme-code-inline-bg); + padding: var(--padding-block) var(--padding-inline); + margin: calc(var(--padding-block) * -1) -0.125em; + border-radius: var(--border-radius); +} + +pre > code:not([class*="language"]) { + background-color: transparent; + padding: 0; + margin: 0; + border-radius: 0; + color: inherit; +} + +pre { + position: relative; + background-color: var(--theme-code-bg); + color: var(--theme-code-text); + --padding-block: 1rem; + --padding-inline: 2rem; + padding: var(--padding-block) var(--padding-inline); + padding-right: calc(var(--padding-inline) * 2); + margin-left: calc(50vw - var(--padding-inline)); + transform: translateX(-50vw); + + line-height: 1.414; + width: calc(100vw + 4px); + max-width: calc(100% + (var(--padding-inline) * 2)); + overflow-y: hidden; + overflow-x: auto; +} + +@media (min-width: 37.75em) { + pre { + --padding-inline: 1.25rem; + border-radius: 8px; + } +} + +blockquote { + margin: 2rem 0; + padding: 0.5em 1rem; + border-left: 3px solid rgba(0, 0, 0, 0.35); + background-color: rgba(0, 0, 0, 0.05); + border-radius: 0 0.25rem 0.25rem 0; +} + +.flex { + display: flex; + align-items: center; +} + +header button { + background-color: var(--theme-bg); +} +header button:hover, +header button:focus { + background: var(--theme-text); + color: var(--theme-bg); +} + +button { + display: flex; + align-items: center; + justify-items: center; + gap: 0.25em; + padding: 0.33em 0.67em; + border: 0; + background: var(--theme-bg); + display: flex; + font-size: 1rem; + align-items: center; + gap: 0.25em; + border-radius: 99em; + background-color: var(--theme-bg); +} +button:hover { + +} + +#theme-toggle { + display: flex; + align-items: center; + gap: 0.25em; + padding: 0.33em 0.67em; + margin-left: -0.67em; + margin-right: -0.67em; + border-radius: 99em; + background-color: var(--theme-bg); +} +#theme-toggle:focus-within { + outline: 2px solid transparent; + box-shadow: 0 0 0 0.08em var(--theme-accent), 0 0 0 0.12em white; +} + +#theme-toggle > label { + position: relative; + display: flex; + align-items: center; + justify-content: center; + font-size: 1.5rem; + width: 1.5rem; + height: 1.5rem; + opacity: 0.5; + transition: transform 120ms ease-out, opacity 120ms ease-out; +} + +#theme-toggle > label:hover, +#theme-toggle > label:focus { + transform: scale(1.125); + opacity: 1; +} + +#theme-toggle .checked { + color: var(--theme-accent); + transform: scale(1.125); + opacity: 1; +} + +input[name="theme-toggle"] { + position: absolute; + opacity: 0; + top: 0; + right: 0; + bottom: 0; + left: 0; + z-index: -1; +} + +nav h4 { + font-weight: 400; + font-size: 1.25rem; + margin: 0; + margin-bottom: 1em; +} + +.edit-on-github, +.header-link { + font-size: 1rem; + padding-left: 1rem; + border-left: 4px solid var(--theme-divider); +} + +.edit-on-github:hover, +.edit-on-github:focus, +.header-link:hover, +.header-link:focus { + color: var(--theme-text-light); + border-left-color: var(--theme-text-lighter); +} + +.header-link:focus-within { + color: var(--theme-text-light); + border-left-color: var(--theme-text-lighter); +} + +.header-link.active { + border-left-color: var(--theme-accent); + color: var(--theme-accent); +} + +.header-link.depth-2 { + font-weight: 600; +} + +.header-link.depth-3 { + padding-left: 2rem; +} +.header-link.depth-4 { + padding-left: 3rem; +} + +.edit-on-github, +.header-link a { + font: inherit; + color: inherit; + text-decoration: none; +} + +.edit-on-github { + margin-top: 2rem; + text-decoration: none; +} +.edit-on-github > * { + text-decoration: none; +} + +.nav-link { + font-size: 1rem; + margin-bottom: 0; + transform: translateX(0); + transition: 120ms transform ease-out; +} + +.nav-link:hover, +.nav-link:focus { + color: var(--theme-text-lighter); + transform: translateX(0.25em); +} + +.nav-link:focus-within { + color: var(--theme-text-lighter); + transform: translateX(0.25em); +} + +.nav-link a { + font: inherit; + color: inherit; + text-decoration: none; +} + +.nav-groups > li + li { + margin-top: 2rem; +} diff --git a/examples/doc/public/theme.css b/examples/doc/public/theme.css new file mode 100644 index 000000000..d381604c8 --- /dev/null +++ b/examples/doc/public/theme.css @@ -0,0 +1,73 @@ +:root { + --font-fallback: -apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif,Apple Color Emoji,Segoe UI Emoji; + --font-body: system-ui, var(--font-fallback); + --font-mono: source-code-pro,Menlo,Monaco,Consolas,'Courier New',monospace; + + --color-white: #FFF; + --color-black: #000014; + + --color-gray-50: #F9FAFB; + --color-gray-100: #F3F4F6; + --color-gray-200: #E5E7EB; + --color-gray-300: #D1D5DB; + --color-gray-400: #9CA3AF; + --color-gray-500: #6B7280; + --color-gray-600: #4B5563; + --color-gray-700: #374151; + --color-gray-800: #1F2937; + --color-gray-900: #111827; + + --color-blue: #3894FF; + --color-blue-rgb: 56,148,255; + --color-green: #17C083; + --color-green-rgb: 23,192,131; + --color-orange: #FF5D01; + --color-orange-rgb: 255,93,1; + --color-purple: #882DE7; + --color-purple-rgb: 136,45,231; + --color-red: #FF1639; + --color-red-rgb: 255,22,57; + --color-yellow: #FFBE2D; + --color-yellow-rgb: 255,190,45; +} + +:root { + color-scheme: light; + --theme-accent: var(--color-blue); + --theme-accent-rgb: var(--color-blue-rgb); + --theme-accent-opacity: 0.1; + --theme-divider: var(--color-gray-100); + --theme-text: var(--color-gray-800); + --theme-text-light: var(--color-gray-600); + --theme-text-lighter: var(--color-gray-400); + --theme-bg: var(--color-white); + --theme-bg-offset: var(--color-gray-100); + --theme-bg-accent: rgba(var(--theme-accent-rgb), var(--theme-accent-opacity)); + --theme-code-inline-bg: var(--color-gray-100); + --theme-code-text: var(--color-gray-100); + --theme-code-bg: var(--color-gray-700); +} + +body { + background: var(--theme-bg); + color: var(--theme-text); +} + +:root.theme-dark { + color-scheme: dark; + --theme-accent-opacity: 0.3; + --theme-divider: var(--color-gray-900); + --theme-text: var(--color-gray-200); + --theme-text-light: var(--color-gray-400); + --theme-text-lighter: var(--color-gray-600); + --theme-bg: var(--color-black); + --theme-bg-offset: var(--color-gray-900); + --theme-code-inline-bg: var(--color-gray-800); + --theme-code-text: var(--color-gray-200); + --theme-code-bg: var(--color-gray-900); +} + +::selection { + color: var(--theme-accent); + background-color: rgba(var(--theme-accent-rgb), var(--theme-accent-opacity)); +} diff --git a/examples/doc/public/theme.js b/examples/doc/public/theme.js new file mode 100644 index 000000000..1296b49b3 --- /dev/null +++ b/examples/doc/public/theme.js @@ -0,0 +1,8 @@ +(() => { + const root = document.documentElement; + if (localStorage.theme === 'dark' || (!('theme' in localStorage) && window.matchMedia('(prefers-color-scheme: dark)').matches)) { + root.classList.add('theme-dark') + } else { + root.classList.remove('theme-dark') + } +})() diff --git a/examples/doc/src/components/ArticleFooter.astro b/examples/doc/src/components/ArticleFooter.astro new file mode 100644 index 000000000..8078e2cc3 --- /dev/null +++ b/examples/doc/src/components/ArticleFooter.astro @@ -0,0 +1,15 @@ +--- +import AvatarList from './AvatarList.astro'; +--- + + + + diff --git a/examples/doc/src/components/AvatarList.astro b/examples/doc/src/components/AvatarList.astro new file mode 100644 index 000000000..aafcb371b --- /dev/null +++ b/examples/doc/src/components/AvatarList.astro @@ -0,0 +1,74 @@ + + + + + diff --git a/examples/doc/src/components/DocSidebar.tsx b/examples/doc/src/components/DocSidebar.tsx new file mode 100644 index 000000000..e792851bc --- /dev/null +++ b/examples/doc/src/components/DocSidebar.tsx @@ -0,0 +1,55 @@ +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(undefined); + + useEffect(() => { + const getItemOffsets = () => { + const titles = document.querySelectorAll('article :is(h2, h3, h4)'); + itemOffsets.current = Array.from(titles).map(title => ({ + id: title.id, + topOffset: title.getBoundingClientRect().top + window.scrollY + })); + } + + const onScroll = () => { + const itemIndex = itemOffsets.current.findIndex(item => item.topOffset > window.scrollY + (window.innerHeight / 3)); + if (itemIndex === 0) { + setActiveId(undefined); + } else if (itemIndex === -1) { + setActiveId(itemOffsets.current[itemOffsets.current.length - 1].id) + } else { + setActiveId(itemOffsets.current[itemIndex - 1].id) + } + } + + getItemOffsets(); + window.addEventListener('resize', getItemOffsets); + window.addEventListener('scroll', onScroll); + + return () => { + window.removeEventListener('resize', getItemOffsets); + window.removeEventListener('scroll', onScroll); + } + }, []); + + return ( + + ); +} + +export default DocSidebar; diff --git a/examples/doc/src/components/EditOnGithub.tsx b/examples/doc/src/components/EditOnGithub.tsx new file mode 100644 index 000000000..e39dafce3 --- /dev/null +++ b/examples/doc/src/components/EditOnGithub.tsx @@ -0,0 +1,13 @@ +import type { FunctionalComponent } from 'preact'; +import { h } from 'preact'; + +const EditOnGithub: FunctionalComponent<{ href: string }> = ({ href }) => { + return ( + + + Edit on GitHub + + ); +} + +export default EditOnGithub; diff --git a/examples/doc/src/components/Note.astro b/examples/doc/src/components/Note.astro new file mode 100644 index 000000000..46940ddf8 --- /dev/null +++ b/examples/doc/src/components/Note.astro @@ -0,0 +1,49 @@ +--- +export let type = "tip"; +export let title; +--- + + + + diff --git a/examples/doc/src/components/SiteSidebar.astro b/examples/doc/src/components/SiteSidebar.astro new file mode 100644 index 000000000..7279d9aea --- /dev/null +++ b/examples/doc/src/components/SiteSidebar.astro @@ -0,0 +1,20 @@ +--- +import { sidebar } from '../config.ts'; +--- + + diff --git a/examples/doc/src/components/ThemeToggle.tsx b/examples/doc/src/components/ThemeToggle.tsx new file mode 100644 index 000000000..d26d94d69 --- /dev/null +++ b/examples/doc/src/components/ThemeToggle.tsx @@ -0,0 +1,65 @@ +import type { FunctionalComponent } from 'preact'; +import { h, Fragment } from 'preact'; +import { useState, useEffect } from 'preact/hooks'; + +const themes = [ + 'system', + 'light', + 'dark' +] + +const icons = [ + + +, + + + , + + + +] + +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
+ {themes.map((t, i) => { + const icon = icons[i]; + const checked = t === theme; + return ( + + ); + })} +
+} + +export default ThemeToggle; diff --git a/examples/doc/src/config.ts b/examples/doc/src/config.ts new file mode 100644 index 000000000..645ea9f61 --- /dev/null +++ b/examples/doc/src/config.ts @@ -0,0 +1,9 @@ +export const sidebar = [ + { + text: 'Introduction', + children: [ + { text: 'Welcome', link: '/' }, + { text: 'Example', link: '/example' } + ] + } +] diff --git a/examples/doc/src/layouts/Main.astro b/examples/doc/src/layouts/Main.astro new file mode 100644 index 000000000..b741098ef --- /dev/null +++ b/examples/doc/src/layouts/Main.astro @@ -0,0 +1,228 @@ +--- +import ArticleFooter from '../components/ArticleFooter.astro'; +import SiteSidebar from '../components/SiteSidebar.astro'; +import ThemeToggle from '../components/ThemeToggle.tsx'; +import DocSidebar from '../components/DocSidebar.tsx'; + +export let content; +const headers = content?.astro?.headers; +let editHref = Astro?.request?.url?.pathname?.slice(1) ?? ''; +if (editHref === '') editHref = `index`; +editHref = `https://github.com/snowpackjs/astro/tree/main/examples/doc/src/pages/${editHref}.md` +--- + + + + {content.title} + + +