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

Back To Top Link #2990

Merged
merged 53 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from 46 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
211f7af
Page template for length
adi-unni Nov 30, 2023
4ac4d89
Add macro
adi-unni Dec 4, 2023
f0b2ca4
Add component foundations
adi-unni Dec 4, 2023
4fd04db
Build stylesheet
adi-unni Dec 4, 2023
2af5d6d
Add additional functionality
adi-unni Dec 4, 2023
af0875d
class list remove issue
alessioventuriniAND Dec 4, 2023
ca36e31
Improved functionality
adi-unni Dec 4, 2023
bb80ba5
Changed functionality to links
adi-unni Dec 6, 2023
11565f4
Merge branch 'main' into feature/1599/back-to-top-link
adi-unni Dec 11, 2023
e2461d5
Change grid layout
adi-unni Dec 11, 2023
67b1bed
Merge branch 'main' into feature/1599/back-to-top-link
adi-unni Jan 2, 2024
6e91881
Improved Styling
adi-unni Jan 4, 2024
3cf2c4a
Attempted fix
adi-unni Jan 4, 2024
9b2a576
Major reworks
adi-unni Jan 8, 2024
a7298cf
For testing
adi-unni Jan 9, 2024
2f5108f
Increased solution elegance
adi-unni Jan 11, 2024
906cf2d
Addition of resize observer
adi-unni Jan 24, 2024
c783b33
Merge branch 'main' into feature/1599/back-to-top-link
adi-unni Jan 24, 2024
76e810d
Fixed flickering
adi-unni Jan 26, 2024
6c1a32c
refresh
adi-unni Jan 26, 2024
b69087a
Merge branch 'main' into feature/1599/back-to-top-link
adi-unni Jan 29, 2024
c2badfc
Fix border and layout
adi-unni Jan 29, 2024
6da6c1a
Fix resize behaviour
adi-unni Jan 30, 2024
9939ce1
Changes to CSS
adi-unni Feb 1, 2024
47e7f16
Test linter
adi-unni Feb 1, 2024
0dde92f
Test linter pt2
adi-unni Feb 1, 2024
91188ef
Test
adi-unni Feb 1, 2024
a09e075
Fix spacing issues
adi-unni Feb 1, 2024
2614e12
Add more tests
adi-unni Feb 1, 2024
2bee753
Merge branch 'main' into feature/1599/back-to-top-link
adi-unni Feb 1, 2024
f6a4100
Macro tests working
adi-unni Feb 1, 2024
6438308
rewrite some tests
adi-unni Feb 5, 2024
43cf070
Merge branch 'main' into feature/1599/back-to-top-link
adi-unni Feb 5, 2024
dc8c9c1
Commit
adi-unni Feb 7, 2024
2126e06
Fixed tests
adi-unni Feb 11, 2024
6072cb1
Merge branch 'main' into feature/1599/back-to-top-link
adi-unni Feb 13, 2024
393f9cf
Update VR Tests
adi-unni Feb 13, 2024
2b6a954
Update git workflow
adi-unni Feb 13, 2024
bc8230a
Improvements according to commnents
adi-unni Feb 19, 2024
b2d9ec9
Merge branch 'main' into feature/1599/back-to-top-link
adi-unni Feb 19, 2024
6b01cf4
Add new test files
adi-unni Feb 19, 2024
04dbc5c
Fixed Tests
adi-unni Feb 19, 2024
ec5605b
Delete old test files
adi-unni Feb 19, 2024
9f26c54
Merge branch 'main' into feature/1599/back-to-top-link
alessioventuriniAND Feb 22, 2024
41a8621
Fixed some whitespace issues
adi-unni Feb 23, 2024
0e3cb6f
Add waits
adi-unni Feb 23, 2024
71a950a
Changed file names
adi-unni Feb 26, 2024
e451262
Align back to top with skip to content
adi-unni Feb 27, 2024
4a4e93b
Merge branch 'main' into feature/1599/back-to-top-link
adi-unni Feb 27, 2024
7c0a9ec
Change to full width
adi-unni Feb 27, 2024
83cbeb2
Remove unused CSS
adi-unni Feb 28, 2024
b76af87
Changes to tabbing
adi-unni Feb 28, 2024
e7d21ab
Merge branch 'main' into feature/1599/back-to-top-link
adi-unni Feb 28, 2024
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
2 changes: 1 addition & 1 deletion .github/workflows/check-labels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ jobs:
steps:
- uses: docker://onsdigital/github-pr-label-checker:v1.2.7
with:
one_of: Breaking changes,Accessibility,Bug,Documentation,Dependencies,Enhancement,Example
one_of: Breaking changes,Accessibility,Bug,Documentation,Dependencies,Enhancement,Example,Component,Pattern
none_of: Awaiting resource,Do not merge,Duplicate,Needs validating,Not doing
repo_token: ${{ secrets.GITHUB_TOKEN }}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
37 changes: 37 additions & 0 deletions src/components/back-to-top/_back-to-top.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
.ons-back-to-top {
&__description {
margin-left: 0.2rem;
}

&__enabled {
background: none;
width: fit-content;
padding: 0.5em 0;

> .ons-back-to-top__link {
padding: 0;
}
}

&__sticky {
bottom: -1px;
position: fixed;
z-index: 10;
background: var(--ons-color-grey-15);
width: 100%;
height: fit-content;
left: 0;

> .ons-back-to-top__link {
padding: 0.7em 0;
position: relative;
display: block;
color: var(--ons-color-black);

&:focus {
box-shadow: none;
text-decoration: underline solid var(--ons-color-text-link-focus) 4px;
}
}
}
}
4 changes: 4 additions & 0 deletions src/components/back-to-top/_macro-options.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
| Name | Type | Required | Description |
| ----------- | ------ | -------- | -------------------------------------------------------------- |
| description | string | false | The text label added to the button. Defaults to "Back to Top" |
| anchor | string | false | The 'id' of the element the button jumps to. Defaults to "Top" |
15 changes: 15 additions & 0 deletions src/components/back-to-top/_macro.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% from "components/icon/_macro.njk" import onsIcon %}

{% macro onsBackToTop(params) %}
<div class="ons-back-to-top ons-js-back-to-top ons-back-to-top__enabled">
<a href="#{{ params.anchor | default('top') }}" class="ons-back-to-top__link">
{{-
onsIcon({
"iconType": 'arrow-up',
"iconSize": 'xs'
})
-}}
<span class="ons-back-to-top__description">{{ params.description | default('Back to top') }}</span>
</a>
</div>
{% endmacro %}
60 changes: 60 additions & 0 deletions src/components/back-to-top/_macro.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/** @jest-environment jsdom */

import * as cheerio from 'cheerio';

import axe from '../../tests/helpers/axe';
import { renderComponent } from '../../tests/helpers/rendering';

describe('macro: back-to-top', () => {
it('passes jest-axe checks', async () => {
const $ = cheerio.load(
renderComponent('back-to-top', {
description: 'Back to top',
anchor: 'example-target',
}),
);

const results = await axe($.html());
expect(results).toHaveNoViolations();
});

it('has back to top link with the provided description', async () => {
const $ = cheerio.load(
renderComponent('back-to-top', {
description: 'Scroll to top',
anchor: 'example-target',
}),
);
expect($('.ons-back-to-top .ons-back-to-top__link').text().trim()).toBe('Scroll to top');
});

it('has back to top link with the provided anchor', async () => {
const $ = cheerio.load(
renderComponent('back-to-top', {
description: 'Back to top',
anchor: 'example-target',
}),
);

expect($('.ons-back-to-top .ons-back-to-top__link').attr('href')).toBe('#example-target');
});

it('has back to top link with the default description if no description provided', async () => {
const $ = cheerio.load(
renderComponent('back-to-top', {
anchor: 'example-target',
}),
);

expect($('.ons-back-to-top .ons-back-to-top__link').text().trim()).toBe('Back to top');
});

it('has back to top link with the default anchor if no anchor provided', async () => {
const renderedComponent = renderComponent('back-to-top', {
description: 'Back to top',
});
const $ = cheerio.load(renderedComponent);

expect($('.ons-back-to-top .ons-back-to-top__link').attr('href')).toBe('#top');
});
});
12 changes: 12 additions & 0 deletions src/components/back-to-top/backToTop.dom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import domready from '../../js/domready';

async function backToTop() {
const bttElement = [...document.querySelectorAll('.ons-js-back-to-top')];

if (bttElement) {
const Btt = (await import('./backToTop')).default;
bttElement.forEach((btn) => new Btt(btn));
}
}

domready(backToTop);
59 changes: 59 additions & 0 deletions src/components/back-to-top/backToTop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
export default class backToTop {
constructor(component) {
this.component = component;
this.content = this.component.previousElementSibling;
this.target = document.getElementById(this.component.firstElementChild.href.split('#')[1]);
this.contentleft, this.contentwidth;
this.updateContentDetails();

this.handleScroll = this.handleScroll.bind(this);
window.addEventListener('scroll', this.handleScroll);

window.addEventListener('resize', () => {
this.setEnabled();
this.updateContentDetails();
this.handleScroll();
});

this.component.addEventListener('click', () => {
this.component.firstElementChild.blur();
});
}

handleScroll() {
let scrollPosition = window.scrollY + window.innerHeight;

if (this.target) {
scrollPosition = -this.target.getBoundingClientRect().top + window.innerHeight;
}

const contentRect = this.content.getBoundingClientRect();
const windowHeight = window.innerHeight;
const contentBottom = contentRect.bottom;

const stickyThreshold = windowHeight * 2;
if (scrollPosition > stickyThreshold && windowHeight < contentBottom) {
this.setSticky();
} else {
this.setEnabled();
}
}

setSticky() {
this.component.classList.remove('ons-back-to-top__enabled');
this.component.classList.add('ons-back-to-top__sticky');
this.component.firstElementChild.style.left = `${this.contentleft}px`;
this.component.firstElementChild.style.width = `${this.contentwidth}px`;
}

setEnabled() {
this.updateContentDetails();
this.component.classList.remove('ons-back-to-top__sticky');
this.component.classList.add('ons-back-to-top__enabled');
}

updateContentDetails() {
this.contentleft = this.component.getBoundingClientRect().left;
this.contentwidth = this.content.offsetWidth;
}
}
Loading
Loading