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

fix(carousel): improved tab page navigation #5035

Merged
Merged
Changes from 6 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
caea15c
fix(megamenu): fixed uncentered arrow icon in safari
IgnacioBecerra Nov 24, 2020
e0827fd
Revert "fix(megamenu): fixed uncentered arrow icon in safari"
IgnacioBecerra Nov 24, 2020
d7c4f8b
Merge branch 'master' of https://github.com/carbon-design-system/carb…
IgnacioBecerra Nov 24, 2020
370161c
Merge branch 'master' of https://github.com/carbon-design-system/carb…
IgnacioBecerra Nov 25, 2020
7ac3c4e
Merge branch 'master' of https://github.com/carbon-design-system/carb…
IgnacioBecerra Nov 30, 2020
b3bf936
Merge branch 'master' of https://github.com/carbon-design-system/carb…
IgnacioBecerra Dec 1, 2020
a9d195d
Merge branch 'master' of https://github.com/carbon-design-system/carb…
IgnacioBecerra Dec 3, 2020
d62d6d4
Merge branch 'master' of https://github.com/carbon-design-system/carb…
IgnacioBecerra Dec 7, 2020
5392a08
Merge branch 'master' of https://github.com/carbon-design-system/carb…
IgnacioBecerra Dec 7, 2020
90a961a
Merge branch 'master' of https://github.com/carbon-design-system/carb…
IgnacioBecerra Dec 8, 2020
41fbdd5
Merge branch 'master' of https://github.com/carbon-design-system/carb…
IgnacioBecerra Dec 9, 2020
95f4af5
Merge branch 'master' of https://github.com/carbon-design-system/carb…
IgnacioBecerra Dec 10, 2020
a9a4511
Merge branch 'master' of https://github.com/IgnacioBecerra/ibm-dotcom…
IgnacioBecerra Dec 15, 2020
671764b
Merge branch 'master' of https://github.com/carbon-design-system/carb…
IgnacioBecerra Dec 16, 2020
e52de62
Merge branch 'master' of https://github.com/carbon-design-system/carb…
IgnacioBecerra Jan 11, 2021
23c0161
Merge branch 'master' of https://github.com/carbon-design-system/carb…
IgnacioBecerra Jan 12, 2021
81132f4
Merge branch 'master' of https://github.com/carbon-design-system/carb…
IgnacioBecerra Jan 15, 2021
227309d
Merge branch 'master' of https://github.com/carbon-design-system/carb…
IgnacioBecerra Jan 19, 2021
910a43c
Merge branch 'master' of https://github.com/carbon-design-system/carb…
IgnacioBecerra Jan 20, 2021
1142e4d
Merge branch 'master' of https://github.com/carbon-design-system/carb…
IgnacioBecerra Jan 27, 2021
00f46f5
fix(carousel): improved tab page navigation
IgnacioBecerra Jan 28, 2021
c06974e
fix(carousel): consolidated logic
IgnacioBecerra Jan 29, 2021
ad92d2f
fix(carousel): updated page calculation
IgnacioBecerra Jan 30, 2021
889721a
fix(footer): updated index calculations
IgnacioBecerra Feb 1, 2021
baf1f3a
fix(carousel): considered focus cases
IgnacioBecerra Feb 2, 2021
6a8689f
Merge branch 'master' of https://github.com/carbon-design-system/carb…
IgnacioBecerra Feb 4, 2021
3a93494
Merge branch 'master' into carousel-focus-fix
asudoh Feb 4, 2021
cacd606
fix(carousel): considered offset page start
IgnacioBecerra Feb 4, 2021
8abb24c
Merge branch 'master' of https://github.com/carbon-design-system/carb…
IgnacioBecerra Feb 4, 2021
58ec236
Merge branch 'master' of https://github.com/carbon-design-system/carb…
IgnacioBecerra Feb 8, 2021
7196136
Merge branch 'carousel-focus-fix' of https://github.com/IgnacioBecerr…
IgnacioBecerra Feb 8, 2021
3db21ec
fix(carousel): consolidated start logic even further
IgnacioBecerra Feb 9, 2021
b4cdb5a
fix(carousel): stop default scroll upon focus
IgnacioBecerra Feb 10, 2021
aea7029
fix(carousel): focus on last card when tabbing back from the bottom
IgnacioBecerra Feb 10, 2021
ef25a3a
fix(carousel): using lit-html template for event listener
IgnacioBecerra Feb 10, 2021
5db8fe7
fix(carousel): added comment explaining logic
IgnacioBecerra Feb 11, 2021
8ca1216
Merge branch 'master' into carousel-focus-fix
IgnacioBecerra Feb 12, 2021
1b9e779
Merge branch 'master' into carousel-focus-fix
kodiakhq[bot] Feb 12, 2021
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
35 changes: 34 additions & 1 deletion packages/web-components/src/components/carousel/carousel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import ddsSettings from '@carbon/ibmdotcom-utilities/es/utilities/settings/setti
import ifNonNull from 'carbon-web-components/es/globals/directives/if-non-null.js';
import CaretLeft20 from 'carbon-web-components/es/icons/caret--left/20.js';
import CaretRight20 from 'carbon-web-components/es/icons/caret--right/20.js';
import HostListener from 'carbon-web-components/es/globals/decorators/host-listener';
import HostListenerMixin from 'carbon-web-components/es/globals/mixins/host-listener';
import DDSCard from '../card/card';
import styles from './carousel.scss';

const { prefix } = settings;
Expand All @@ -26,7 +29,7 @@ const { stablePrefix: ddsPrefix } = ddsSettings;
* @csspart next-button The button to go to the next page.
*/
@customElement(`${ddsPrefix}-carousel`)
class DDSCarousel extends LitElement {
class DDSCarousel extends HostListenerMixin(LitElement) {
/**
* The scrolling contents node.
*/
Expand Down Expand Up @@ -111,6 +114,36 @@ class DDSCarousel extends LitElement {
}
}

/**
* Handles card focus throughout pages.
*
* @param event The event.
*/
@HostListener('shadowRoot:focusin')
// @ts-ignore: The decorator refers to this method but TS thinks this method is not referred to
private _handleFocus = async ({ target, relatedTarget }: FocusEvent) => {
const currentContains = target !== this && this.contains(target as DDSCard);
const oldNotContains = target !== this && !this.contains(relatedTarget as DDSCard);
const currentCardIndex = Array.from(this.children).indexOf(target as HTMLElement);

// keep current page if tabbing back into the carousel after previously moving pages
if (currentContains && oldNotContains && currentCardIndex === 0) {
(this.children[this.start] as HTMLElement).focus();
return;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need this logic?

Copy link
Contributor Author

@IgnacioBecerra IgnacioBecerra Feb 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@asudoh Yes, we still need that logic to focus on the current starting card after having tabbed outside the carrousel. Without it, the focus will always go to the first card automatically, out of view. This will focus on the current page's starting card.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, thank you for clarifying - I suggest preserving the "focused card index" in _handleFocus(). It can be done by "old contains but current node contains" detection and preserving event.relatedTarget. In this way we can set this.start according to the index of such "previously focused card".


if (currentContains) {
// going forwards, change page depending on card index
if (currentCardIndex >= this.start + this.pageSize) {
this.start = currentCardIndex - (currentCardIndex % this.pageSize);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering the following condition:

  • We have many cards in carousel
  • this.start previously was 2
  • Screen size as large as putting 4 cards in a page

And user keeps hitting tab key to reach to the code here. What should happen?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@asudo I see, wasn't aware the this.start could be set by the user. Added an additional calculation where it considers the offset to calculate the right index if this.start isn't a proper multiple of this.pageSize.

Now in the condition you provided this.start will become the index 6 instead of 4 as it was before i.e. the next page will start with the proper card.


// going backwards, change page depending on card index
} else if (currentCardIndex < this.start) {
this.start = Math.max(currentCardIndex + 1 - this.pageSize, 0);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to apply your improvement to "page forward" logic to the "page backward" logic here, too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we'd use the same code going in either direction, the previous logic runs in both directions whenever the focus is out of the current page.

}
}
};

/**
* Handles `click` event on the next button.
*/
Expand Down