Skip to content

Commit 2615627

Browse files
authored
Merge pull request #473 from BeAPI/fix/skip-links-mobile
Fix/skip links mobile
2 parents ea5bc9a + 7fc7e77 commit 2615627

File tree

7 files changed

+85
-5
lines changed

7 files changed

+85
-5
lines changed

header.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,12 @@
2727
<?php wp_body_open(); ?>
2828
<nav class="skip-links skip-links--hidden" aria-label="<?php esc_attr_e( 'Fast access links', 'beapi-frontend-framework' ); ?>">
2929
<ul>
30-
<li>
30+
<li class="is-desktop-only">
3131
<a href="#menu"><?php esc_html_e( 'Go to main navigation menu', 'beapi-frontend-framework' ); ?></a>
3232
</li>
33+
<li class="is-mobile-only">
34+
<a href="#menu-toggle"><?php esc_html_e( 'Go to main navigation menu', 'beapi-frontend-framework' ); ?></a>
35+
</li>
3336
<li>
3437
<a href="#content"><?php esc_html_e( 'Go to main content', 'beapi-frontend-framework' ); ?></a>
3538
</li>
@@ -46,7 +49,7 @@
4649
<span class="sr-only"><?php echo esc_html( get_bloginfo( 'name' ) ); ?></span>
4750
</a>
4851
<?php if ( has_nav_menu( 'menu-main' ) ) : ?>
49-
<button class="header__menu-toggle" aria-expanded="false" aria-controls="menu">
52+
<button id="menu-toggle" class="header__menu-toggle" aria-expanded="false" aria-controls="menu">
5053
<span></span>
5154
<span class="sr-only aria-expanded-false-text"><?php esc_html_e( 'Open the menu', 'beapi-frontend-framework' ); ?></span>
5255
<span class="sr-only aria-expanded-true-text"><?php esc_html_e( 'Close the menu', 'beapi-frontend-framework' ); ?></span>

src/js/utils/getCssVar.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* Get the value from a CSS custom property.
3+
*
4+
* @param {string} name CSS custom property name (ex: '--breakpoint-mobile-to-desktop-nav').
5+
* @param {HTMLElement} element The element to get the CSS custom property from.
6+
* @return {string} The value.
7+
* @example getCssVar('--breakpoint-mobile-to-desktop-nav') => '1200'
8+
*/
9+
export default function getCssVar(name, element = document.documentElement) {
10+
if (!name) {
11+
console.warn('getCssVar: No name provided.')
12+
return ''
13+
}
14+
15+
const propName = name.startsWith('--') ? name : `--${name}`
16+
17+
return getComputedStyle(element).getPropertyValue(propName).trim()
18+
}

src/js/utils/isMobileNav.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import getCssVar from './getCssVar'
2+
3+
/**
4+
* Check if the current viewport is mobile based on a CSS breakpoint.
5+
*
6+
* @param {string} breakpointVar CSS custom property name for the breakpoint.
7+
* @return {boolean} True if viewport is mobile, false otherwise.
8+
* @example isMobileNav() => true; !isMobileNav() => false
9+
*/
10+
export default function isMobileNav(breakpointVar = '--breakpoint-mobile-to-desktop-nav') {
11+
const rawValue = getCssVar(breakpointVar)
12+
13+
if (!rawValue) {
14+
console.warn(`isMobileNav: Variable ${breakpointVar} is empty or undefined. Returning false.`)
15+
return false
16+
}
17+
18+
const breakpoint = parseInt(rawValue, 10)
19+
20+
if (isNaN(breakpoint)) {
21+
console.warn(`isMobileNav: Could not parse "${rawValue}" as a number.`)
22+
return false
23+
}
24+
25+
return window.matchMedia(`(max-width: ${breakpoint - 1}px)`).matches
26+
}

src/scss/01-abstract/_variables.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ $breakpoints: (
5353
admin-bar: 784, // admin bar height change
5454
m: 960,
5555
md: 1080,
56+
mobile-to-desktop-nav: 1200,
5657
mdl: 1279, // Do not use 1280px, it causes a bug under Window Edge with a device pixel ratio higher than 1
5758
l: 1440,
5859
);

src/scss/03-base/_variables-css.scss

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55

66
:root {
77
/*
8+
* Breakpoints
9+
*/
10+
--breakpoint-mobile-to-desktop-nav: #{map-get($breakpoints, mobile-to-desktop-nav)}; // Used by JavaScript
11+
12+
/*
813
* Heading
914
*/
1015
// Font size
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
@use "../02-tools/m-breakpoint" as *;
2+
@use "../02-tools/m-style-only" as *;
3+
4+
/**
5+
* Display utilities
6+
*
7+
* Utility classes for showing/hiding elements based on the mobile-to-desktop-nav breakpoint.
8+
* Used primarily for skip links and navigation-related elements.
9+
*/
10+
11+
// Visible only on mobile (below mobile-to-desktop-nav breakpoint)
12+
.is-mobile-only {
13+
@include breakpoints(mobile-to-desktop-nav) {
14+
@include style-only {
15+
display: none !important;
16+
}
17+
}
18+
}
19+
20+
// Visible only on desktop (at or above mobile-to-desktop-nav breakpoint)
21+
.is-desktop-only {
22+
@include breakpoints(mobile-to-desktop-nav, max) {
23+
@include style-only {
24+
display: none !important;
25+
}
26+
}
27+
}

src/scss/08-template-parts/_header.scss

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@
180180
}
181181
}
182182

183-
@include breakpoints(mdl, max) {
183+
@include breakpoints(mobile-to-desktop-nav, max) {
184184
&__menu {
185185
position: absolute;
186186
top: 0;
@@ -229,7 +229,7 @@
229229
}
230230
}
231231

232-
@include breakpoints(sm, mdl) {
232+
@include breakpoints(sm, mobile-to-desktop-nav) {
233233
#{$el}__menu {
234234
right: 0;
235235
left: auto;
@@ -248,7 +248,7 @@
248248
}
249249
}
250250

251-
@include breakpoints(mdl) {
251+
@include breakpoints(mobile-to-desktop-nav) {
252252
.container {
253253
display: flex;
254254
align-items: flex-start;

0 commit comments

Comments
 (0)