Skip to content

Commit bc41e69

Browse files
authored
fix <details> in blog posts (#335)
* WIP restyle details in blog posts * style details elements in blog posts
1 parent a29dc72 commit bc41e69

File tree

3 files changed

+63
-63
lines changed

3 files changed

+63
-63
lines changed

apps/svelte.dev/content/blog/2023-08-31-view-transitions.md

Lines changed: 50 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -76,32 +76,28 @@ With that, every navigation that occurs will trigger a view transition. You can
7676

7777
<video src="https://sveltejs.github.io/assets/video/vt-demo-1.mp4" controls muted playsinline></video>
7878

79-
<details>
80-
<summary>How the code works</summary>
81-
82-
This code may look a bit intimidating – if you're curious, I can break it down line-by-line, but for now it’s enough to know that adding it will allow you to interact with the view transitions API during navigation.
83-
84-
As mentioned above, the `onNavigate` callback will run immediately before the new page is rendered after a navigation. Inside the callback, we check if `document.startViewTransition` exists. If it doesn’t (i.e. the browser doesn’t support it), we exit early.
85-
86-
We then return a promise to delay completing the navigation until the view transition has started. We use a [promise constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/Promise) so that we can control when the promise resolves.
87-
88-
```js
89-
// @errors: 1108
90-
return new Promise((resolve) => {
91-
document.startViewTransition(async () => {
92-
resolve();
93-
await navigation.complete;
94-
});
95-
});
96-
```
97-
98-
Inside the promise constructor, we start the view transition. Inside the view transition callback we resolve the promise we just returned, which indicates to SvelteKit that it should finish the navigation. It’s important that the navigation waits to finish until _after_ we start the view transition – the browser needs to snapshot the old state so it can transition to the new state.
99-
100-
Finally, inside the view transition callback we wait for SvelteKit to finish the navigation by awaiting `navigation.complete`. Once `navigation.complete` resolves, the new page has been loaded into the DOM and the browser can animate between the two states.
101-
102-
It’s a bit of a mouthful, but by not abstracting it we allow you to interact with the view transition directly and make any customizations you require.
103-
104-
</details>
79+
> [!DETAILS] How the code works
80+
> This code may look a bit intimidating – if you're curious, I can break it down line-by-line, but for now it’s enough to know that adding it will allow you to interact with the view transitions API during navigation.
81+
>
82+
> As mentioned above, the `onNavigate` callback will run immediately before the new page is rendered after a navigation. Inside the callback, we check if `document.startViewTransition` exists. If it doesn’t (i.e. the browser doesn’t support it), we exit early.
83+
>
84+
> We then return a promise to delay completing the navigation until the view transition has started. We use a [promise constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/Promise) so that we can control when the promise resolves.
85+
>
86+
> ```js
87+
> // @errors: 1108
88+
> return new Promise((resolve) => {
89+
> document.startViewTransition(async () => {
90+
> resolve();
91+
> await navigation.complete;
92+
> });
93+
> });
94+
> ```
95+
>
96+
> Inside the promise constructor, we start the view transition. Inside the view transition callback we resolve the promise we just returned, which indicates to SvelteKit that it should finish the navigation. It’s important that the navigation waits to finish until > _after_ we start the view transition – the browser needs to snapshot the old state so it can transition to the new state.
97+
>
98+
> Finally, inside the view transition callback we wait for SvelteKit to finish the navigation by awaiting `navigation.complete`. Once `navigation.complete` resolves, the new page has been loaded into the DOM and the browser can animate between the two states.
99+
>
100+
> It’s a bit of a mouthful, but by not abstracting it we allow you to interact with the view transition directly and make any customizations you require.
105101
106102
## Customizing the transition with CSS
107103
@@ -163,38 +159,34 @@ Now, the header will not transition in and out on navigation, but the rest of th
163159

164160
<video src="https://sveltejs.github.io/assets/video/vt-demo-2.mp4" controls muted playsinline></video>
165161

166-
<details>
167-
<summary>Fixing the types</summary>
168-
169-
Since `startViewTransition` is not supported by all browsers, your IDE may not know that it exists. To make the errors go away and get the correct typings, add the following to your `app.d.ts`:
170-
171-
```ts
172-
declare global {
173-
// preserve any customizations you have here
174-
namespace App {
175-
// interface Error {}
176-
// interface Locals {}
177-
// interface PageData {}
178-
// interface Platform {}
179-
}
180-
181-
// add these lines
182-
interface ViewTransition {
183-
updateCallbackDone: Promise<void>;
184-
ready: Promise<void>;
185-
finished: Promise<void>;
186-
skipTransition: () => void;
187-
}
188-
189-
interface Document {
190-
startViewTransition(updateCallback: () => Promise<void>): ViewTransition;
191-
}
192-
}
193-
194-
export {};
195-
```
196-
197-
</details>
162+
> [!DETAILS] Fixing the types
163+
> Since `startViewTransition` is not supported by all browsers, your IDE may not know that it exists. To make the errors go away and get the correct typings, add the following to your `app.d.ts`:
164+
>
165+
> ```ts
166+
> declare global {
167+
> // preserve any customizations you have here
168+
> namespace App {
169+
> // interface Error {}
170+
> // interface Locals {}
171+
> // interface PageData {}
172+
> // interface Platform {}
173+
> }
174+
>
175+
> // add these lines
176+
> interface ViewTransition {
177+
> updateCallbackDone: Promise<void>;
178+
> ready: Promise<void>;
179+
> finished: Promise<void>;
180+
> skipTransition: () => void;
181+
> }
182+
>
183+
> interface Document {
184+
> startViewTransition(updateCallback: () => Promise<void>): ViewTransition;
185+
> }
186+
> }
187+
>
188+
> export {};
189+
> ```
198190
199191
## Transitioning individual elements
200192

packages/site-kit/src/lib/components/Text.svelte

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,9 @@
438438
}
439439
}
440440
441-
details.legacy {
441+
details {
442+
position: relative;
443+
442444
&::before,
443445
&::after {
444446
content: '';
@@ -487,16 +489,15 @@
487489
align-items: center;
488490
height: 3rem;
489491
color: var(--sk-text-4);
490-
font-family: var(--sk-font-ui);
491492
font-style: normal;
492-
font-size: var(--sk-font-size-ui-small);
493+
font-size: var(--sk-font-size-body-small);
493494
user-select: none;
494495
495496
&:hover {
496497
color: var(--sk-text-3);
497498
}
498499
499-
&::after {
500+
.legacy &::after {
500501
position: absolute;
501502
display: flex;
502503
align-items: center;
@@ -516,7 +517,7 @@
516517
& > summary {
517518
margin-bottom: 1.4rem;
518519
519-
&::after {
520+
.legacy &::after {
520521
content: 'hide all';
521522
}
522523
}

packages/site-kit/src/lib/markdown/renderer.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,13 @@ export async function render_content_markdown(
284284
content = `<details class="legacy"><summary>Legacy mode</summary>${content.replace('[!LEGACY]', '')}</details>`;
285285
}
286286

287+
if (content.includes('[!DETAILS]')) {
288+
const regex = /\[!DETAILS\] (.+)/;
289+
const match = regex.exec(content)!;
290+
content = `<details><summary>${match[1]}</summary>${content.replace(regex, '')}</details>`;
291+
return `<blockquote class="note">${content}</blockquote>`;
292+
}
293+
287294
if (content.includes('[!NOTE]')) {
288295
return `<blockquote class="note">${content.replace('[!NOTE]', '')}</blockquote>`;
289296
}

0 commit comments

Comments
 (0)