Skip to content

Commit fcd4631

Browse files
authored
[css-navigation-1] Add a bunch of examples. (#13154)
(Also fix an edit I missed adding support for named routes declared by @route to :link-to().)
1 parent faaa5d8 commit fcd4631

File tree

1 file changed

+122
-2
lines changed

1 file changed

+122
-2
lines changed

css-navigation-1/Overview.bs

Lines changed: 122 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,48 @@ Negation, conjunction, and disjunction are all needed
132132
so that authors can specify the interaction of multiple styles
133133
in ways that are most intuitive and require the simplest code.
134134

135+
<div class="example">
136+
137+
The ''@navigation'' rule can be used in simple cases
138+
to define styles that only affect a particular page:
139+
140+
<pre highlight="css">
141+
@navigation (at: url-pattern("/")) {
142+
/* These styles only apply to the site's homepage
143+
(including any URL with a search or hash). */
144+
}
145+
</pre>
146+
147+
</div>
148+
149+
<div class="example">
150+
151+
The ''@navigation'' rule can also be used to define styles
152+
that are used when a certain navigation is in progress.
153+
This is particularly useful for defining
154+
styles that cause [=view transitions=].
155+
156+
<pre highlight="css">
157+
@route --search-results-page {
158+
pattern: url-pattern("/search-results");
159+
}
160+
@route --product-page {
161+
pattern: url-pattern("/product/:id");
162+
}
163+
164+
@navigation ((from: --search-results-page) and
165+
(to: --product-page)) or
166+
((from: --product-page) and
167+
(to: --search-results-page)) {
168+
/* These styles apply when a navigation is in progress
169+
between a search results page and a product page (as
170+
defined by the @route rules above), in either
171+
direction. */
172+
}
173+
</pre>
174+
175+
</div>
176+
135177
The syntax of the ''@navigation'' rule is:
136178

137179
<pre class="prod def" nohighlight>
@@ -350,20 +392,93 @@ ISSUE: This should probably have a more formal definition of the function,
350392
but I can't find the formal definitions of the existing ''if()'' functions
351393
to model it after.
352394

353-
<h2 id="link-navigation-pseudo-classes">Pseudo-classes for navigation-related links</h2>
395+
<h2 id="link-navigation-pseudo-classes">Pseudo-class for navigation-related links: '':link-to()''</h2>
354396

355397
This specification defines a new
356398
<dfn id="link-to-pseudo" selector>'':link-to()''</dfn> functional pseudo-class
357399
that matches link elements that link to a certain URL.
358400

401+
<div class="example">
402+
403+
A simple example of a ''::link-to()'' selector is this one,
404+
which matches any links that link to the site's homepage:
405+
406+
<pre highlight=css>
407+
:link-to(url-pattern("/")) {
408+
font-weight: bold;
409+
}
410+
</pre>
411+
412+
</div>
413+
414+
<div class="example">
415+
416+
A more interesting example of the ''::link-to()'' pseudo-class
417+
is this example which creates a view transition between
418+
a item in a list that contains a link (in this document)
419+
and the details page for that link (in a different document).
420+
This transition works even when the navigation is a back/forward navigation
421+
and even if the user has used a language selector UI
422+
to change the page into a different language (and thus change the URL).
423+
The use of the '':link-to()'' pseudo-class ensures that
424+
the view transition animations from or to the correct item in the list
425+
by matching the relevant parts of the navigation URL to the link URL.
426+
427+
<pre highlight=css>
428+
@view-transition {
429+
/* allow cross-document view transitions */
430+
navigation: auto;
431+
}
432+
433+
@route --movie-details {
434+
/* match URLs like /en/movie/123 which is the English page
435+
about a movie with ID 123 */
436+
pattern: url-pattern("/:lang/movie/:id");
437+
}
438+
439+
/* capture the overall area representing the movie, and a
440+
sub-area for its poster image */
441+
442+
/* match an element with class movie-container with a child
443+
link that links to a movie whose id is the same as the
444+
movie we are currently navigating to or from. (lang can
445+
be different, though.)
446+
447+
Just :link-to(--movie-details) requires that the target
448+
of the link match the URL pattern defined by the "@route
449+
--movie-details" rule.
450+
451+
The navigation-param(id) further requires that either the
452+
from or the to URL of the current navigation also match
453+
the URL pattern represented by the "@route
454+
--movie-details" rule, and that that the 'id' named group
455+
from that match be the same as the 'id' named group from
456+
the match with the link's target.
457+
*/
458+
.movie-container:has(> .movie-title:link-to(
459+
--movie-details with (navigation-param(id)))) {
460+
461+
view-transition-name: movie-container;
462+
463+
> .movie-poster {
464+
view-transition-name: movie-poster;
465+
}
466+
467+
/* leave the default cross-fade animation for both image
468+
captures */
469+
}
470+
</pre>
471+
472+
</div>
473+
359474
The '':link-to()'' pseudo-class takes a single argument, a <<link-condition>>,
360475
and the pseudo-class matches any element where:
361476
* the element matches '':any-link''
362477
* the target of link matches the <<link-condition>>, as defined below.
363478

364479
<pre class="prod def" dfn-type="type" nohighlight>
365480
<dfn><<link-condition>></dfn> = <<link-condition-base>> [ with <<navigation-param-expression>> ]?
366-
<dfn><<link-condition-base>></dfn> = <<url-pattern()>>
481+
<dfn><<link-condition-base>></dfn> = <<navigation-location>>
367482
<dfn><<navigation-param-expression>></dfn> = ( [ <<navigation-param-and>> |
368483
<<navigation-param-or>> |
369484
<<navigation-param>> ] )
@@ -376,6 +491,9 @@ and the pseudo-class matches any element where:
376491
<dfn><<navigation-param-function>></dfn> = navigation-param( <<ident>> )
377492
</pre>
378493

494+
ISSUE: This grammar should be restructured so that
495+
it doesn't require parentheses around a lone <<navigation-param-function>>.
496+
379497
A <<link-condition>> matches the target of the link when both:
380498
* the <<link-condition-base>> matches the target of the link, and
381499
* the <<navigation-param-expression>> matches the target of the link,
@@ -386,6 +504,8 @@ If the <<link-condition-base>> is a <<url-pattern()>>,
386504
then it represents the URL pattern
387505
represented by the <<url-pattern()>> function
388506
(see [=create a URL pattern for url-pattern()=]).
507+
If it is a <<route-name>>, then it represents the URL pattern
508+
represented by the ''@route'' rule.
389509

390510
A <<link-condition-base>> matches a URL
391511
when [=URL pattern/match|match a URL pattern=] is non-null

0 commit comments

Comments
 (0)