Skip to content

Commit d6db274

Browse files
authored
[css-overflow-5] Expand rationale in carousel explainer. (#13078)
1 parent b4b3427 commit d6db274

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

css-overflow-5/carousel-explainer.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,96 @@ ul::column::scroll-marker {
263263
}
264264
```
265265

266+
### Advantages of pseudo-elements
267+
268+
One of the alternatives considered was to use regular HTML elements. Everything will be possible to build using regular HTML elements with [scroll commands](https://github.com/openui/open-ui/issues/1220#issuecomment-3497651863) and the [scroll-target-group property](https://drafts.csswg.org/css-overflow-5/#scroll-target-group). However, there are several unique advantages to supporting the generation and use of pseudo-elements for carousel components:
269+
270+
#### Semantic content-based HTML with adaptable presentation
271+
272+
By being able to have the buttons and markers out of the DOM, the developer can focus on the semantic content being consumed. E.g. the author can simply write the semantic DOM for a list of tracks and have the stylesheet present that appropriately for the device media with generated controls for scrolling (analogous to an advanced scrollbar).
273+
274+
##### Example
275+
276+
```html
277+
<h3>Album track list</h3>
278+
<ol>
279+
<li><track-cover data-name="Track 1" data-image="track1.png"></track-cover></li>
280+
<li><track-cover data-name="Track 2" data-image="track2.png"></track-cover></li>
281+
<li><track-cover data-name="Track 3" data-image="track3.png"></track-cover></li>
282+
<li><track-cover data-name="Track 4" data-image="track4.png"></track-cover></li>
283+
</ol>
284+
```
285+
286+
Then in the CSS they can customize the presentation to be a carousel on screens, or a list otherwise (e.g. when printed):
287+
288+
```css
289+
@media only screen {
290+
/* On screens, the content is presented in an interactive carousel */
291+
ol {
292+
overflow-x: auto;
293+
scroll-marker-group: after;
294+
scroll-snap-type: x mandatory;
295+
}
296+
track-cover::scroll-marker {
297+
content: "" / attr(data-name);
298+
background-image: attr(data-image);
299+
}
300+
}
301+
```
302+
303+
#### Automatic linkage to related content
304+
305+
When using pure HTML elements, the link from marker to content is implicit \- it is the owning element. A scroll marker is an auto-generated [self-link](https://github.com/w3c/csswg-drafts/issues/10498). Unlike anchor links, the author does not need to set up globally unique identifier strings and be careful to keep those in sync across the two locations. E.g. to create the same carousel as above in html, the author would need to define globally unique identifiers for each of the linked items and copy over needed data such as images in this case:
306+
307+
##### Example
308+
309+
```html
310+
<h3>Album track list carousel</h3>
311+
<div class=carousel>
312+
<ol>
313+
<li><track-cover id="track1" data-name="Track 1" data-image="track1.png"></track-cover></li>
314+
<li><track-cover id="track2" data-name="Track 2" data-image="track2.png"></track-cover></li>
315+
<li><track-cover id="track3" data-name="Track 3" data-image="track3.png"></track-cover></li>
316+
<li><track-cover id="track4" data-name="Track 4" data-image="track4.png"></track-cover></li>
317+
</ol>
318+
<div class="markers">
319+
<a href="#track1"><img src="track1.png"></a>
320+
<a href="#track2"><img src="track2.png"></a>
321+
<a href="#track3"><img src="track3.png"></a>
322+
<a href="#track4"><img src="track4.png"></a>
323+
</div>
324+
</div>
325+
```
326+
327+
Note: This example does not include the necessary accessibility roles and semantics as these could be generated given new HTML elements and the semantic id linkage so it would not necessarily require additional developer work to specify.
328+
329+
#### Automatic pagination
330+
331+
Arguably the most unique and significant advantage is that since pseudo-elements can be generated dynamically based on layout (e.g. a marker per ::column), a developer can create a paginated interface. Currently, Javascript libraries have to add resize observers and carefully generate elements and reparent contents every time layout changes to the correct number of "page" elements. Not only is this difficult to write correctly, and slow, but there is a long history of challenges with reparenting elements without undesirable side effects (e.g. losing focus, selection, state, etc).
332+
333+
##### Example
334+
335+
Going back to our track list, to group the tracks into pages with CSS pseudo-element markers, the author doesn't need to change the HTML, and can add the following CSS:
336+
337+
```css
338+
@media only screen {
339+
/* On screens, the content is grouped into horizontal pages */
340+
ol {
341+
overflow-x: auto;
342+
scroll-marker-group: after;
343+
scroll-snap-type: x mandatory;
344+
columns: 1;
345+
counter-reset: --page;
346+
}
347+
ol::column::scroll-marker {
348+
counter-increment: --page;
349+
content: counter(--page) / "Page " counter(--page);
350+
}
351+
}
352+
```
353+
354+
To do this with custom HTML carousel elements, you would have to reparent the elements into their corresponding pages and change the DOM to have the correct number of marker elements to match, linked to those elements.
355+
266356
## Use cases
267357

268358
### Carousels

0 commit comments

Comments
 (0)