Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(skip-link): href attr #1813

Merged
merged 4 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .changeset/slick-ants-cover.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
"@rhds/elements": patch
---
`<rh-skip-link>`: ensure link is always at top of the page, per guidelines
17 changes: 17 additions & 0 deletions .changeset/tall-corners-punch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
"@rhds/elements": minor
---
`<rh-skip-link>`: added optional `href` attribute:

```html
<rh-skip-link href="#main-content">Skip to main content</rh-skip-link>
```

is equivalent to:

```html
<rh-skip-link>
<a href="#main-content">Skip to main content</a>
</rh-skip-link>
```

2 changes: 1 addition & 1 deletion docs/_includes/layouts/base.njk
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
</head>

<body dsd-pending class="{{ extraPageClasses }}">
<rh-skip-link><a href="#main">Skip to main content</a></rh-skip-link>
<rh-skip-link href="#main">Skip to main content</rh-skip-link>
{{ content | safe }}
</body>
</html>
8 changes: 8 additions & 0 deletions elements/rh-skip-link/demo/href-attribute.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<rh-skip-link href="#main-content">Skip to main content</rh-skip-link>

<h1>Skip link</h1>
<p>To show the skip link, click the white background and press your tab key.</p>
bennypowers marked this conversation as resolved.
Show resolved Hide resolved

<script type="module">
import '@rhds/elements/rh-skip-link/rh-skip-link.js';
</script>
21 changes: 18 additions & 3 deletions elements/rh-skip-link/rh-skip-link.css
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
#container {
display: block !important;
position: relative !important;
display: block;
z-index: 1000;
}

a#container {
position: fixed;
}

div#container {
position: relative;
}

a,
::slotted(a) {
background-color: var(--rh-color-surface-lightest, #ffffff) !important;
border-block-start-width: 0 !important;
Expand Down Expand Up @@ -31,7 +40,6 @@
outline: 0 !important;
overflow: hidden !important;
padding: 0 !important;
position: absolute !important;
text-decoration: none !important;
top: -40px !important;
transform: translateX(-50%) !important;
Expand All @@ -40,6 +48,11 @@
width: 1px !important;
}

::slotted(a) {
position: fixed !important;
}

a:is(:active, :focus),
::slotted(a:is(:active, :focus)) {
border-width: var(--rh-border-width-md, 2px) !important;
clip: auto !important;
Expand All @@ -52,11 +65,13 @@
width: auto !important;
}

a:is(:active, :hover),
::slotted(a:is(:active, :hover)) {
text-decoration: underline !important;
}

@media (prefers-reduced-motion: reduce) {
a,
::slotted(a) {
transition-duration: 0.001ms !important;
}
Expand Down
16 changes: 10 additions & 6 deletions elements/rh-skip-link/rh-skip-link.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { LitElement, html } from 'lit';
import { customElement } from 'lit/decorators/custom-element.js';

import { property } from 'lit/decorators/property.js';

import styles from './rh-skip-link.css';

/**
Expand All @@ -10,19 +12,21 @@ import styles from './rh-skip-link.css';
*
* @summary Skip to the main content of a page
*
* @slot - Place an anchor tag with an `href` that targets an ID of the main content on the page.
* @slot - An anchor tag targeting the main page content by id hash.
* Or, if the `href` attribute is set, the text of the link.
*/
@customElement('rh-skip-link')
export class RhSkipLink extends LitElement {
static shadowRootOptions = { ...LitElement.shadowRootOptions, delegatesFocus: true };

static readonly styles = [styles];

@property({ reflect: true }) href?: string;

render() {
return html`
<div id="container">
<slot></slot>
</div>
`;
return this.href ?
html`<a id="container" href="${this.href}"><slot></slot></a>`
: html`<div id="container"><slot></slot></div>`;
}
}

Expand Down
Loading