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

Add tabIndex as a configurable option. #695

Merged
merged 2 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
</div>
<div class="simplebar-mask">
<div class="simplebar-offset">
<div class="simplebar-content-wrapper" tabIndex="0" role="region" [attr.aria-label]="ariaLabel">
<div class="simplebar-content-wrapper" [attr.tabIndex]="tabIndex" role="region" [attr.aria-label]="ariaLabel">
<div class="simplebar-content">
<ng-content></ng-content>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,14 @@ export class SimplebarAngularComponent implements OnInit, AfterViewInit {
elRef: ElementRef;
SimpleBar: any;
ariaLabel: string;
tabIndex: string;

constructor(elRef: ElementRef, private zone: NgZone) {
this.elRef = elRef;
this.ariaLabel =
this.options.ariaLabel || SimpleBar.defaultOptions.ariaLabel;
this.tabIndex =
(this.options.tabIndex || SimpleBar.defaultOptions.tabIndex).toString();
}

ngOnInit() {}
Expand Down
2 changes: 2 additions & 0 deletions packages/simplebar-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ interface Options {
scrollbarMaxSize: number;
classNames: Partial<ClassNames>;
ariaLabel: string;
tabIndex: number;
scrollableNode: HTMLElement | null;
contentNode: HTMLElement | null;
autoHide: boolean;
Expand Down Expand Up @@ -126,6 +127,7 @@ export default class SimpleBarCore {
scrollbarMinSize: 25,
scrollbarMaxSize: 0,
ariaLabel: 'scrollable content',
tabIndex: 0,
classNames: {
contentEl: 'simplebar-content',
contentWrapper: 'simplebar-content-wrapper',
Expand Down
2 changes: 1 addition & 1 deletion packages/simplebar-react/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const SimpleBar = React.forwardRef<SimpleBarCore | null, Props>(
className: `${classNames.contentWrapper}${
scrollableNodeProps.className ? ` ${scrollableNodeProps.className}` : ''
}`,
tabIndex: 0,
tabIndex: options.tabIndex || SimpleBarCore.defaultOptions.tabIndex,
role: 'region',
'aria-label': options.ariaLabel || SimpleBarCore.defaultOptions.ariaLabel,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ exports[`renders with options 1`] = `
class="simplebar-content-wrapper"
role="region"
style="height: auto; overflow-x: hidden; overflow-y: scroll;"
tabindex="0"
tabindex="-1"
>
<div
class="simplebar-content"
Expand Down
2 changes: 1 addition & 1 deletion packages/simplebar-react/tests/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ test('renders without crashing', () => {

test('renders with options', () => {
const { container } = render(
<SimpleBar forceVisible="y">
<SimpleBar forceVisible="y" tabIndex={-1}>
{[...Array(5)].map((x, i) => (
<p key={i}>Some content</p>
))}
Expand Down
11 changes: 9 additions & 2 deletions packages/simplebar-vue/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ function renderFn({ h, emit, slots, props }: any) {
? {
onScroll,
class: classNames.contentWrapper,
tabIndex: 0,
tabIndex: props.tabIndex ||
SimpleBarCore.defaultOptions.tabIndex,
role: 'region',
'aria-label':
props.ariaLabel ||
Expand All @@ -79,7 +80,8 @@ function renderFn({ h, emit, slots, props }: any) {
: {
attrs: {
class: classNames.contentWrapper,
tabIndex: 0,
tabIndex: props.tabIndex ||
SimpleBarCore.defaultOptions.tabIndex,
role: 'region',
'aria-label':
props.ariaLabel ||
Expand Down Expand Up @@ -162,6 +164,11 @@ export default defineComponent({
*/
ariaLabel: String,

/**
* Set custom tabIndex attribute.
*/
tabIndex: Number,

/**
* Activate RTL support by passing `'rtl'`.
* You will also need a css rule with `direction: rtl`.
Expand Down
5 changes: 5 additions & 0 deletions packages/simplebar-vue/simplebar-vue.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ declare module 'simplebar-vue' {
*/
ariaLabel?: string;

/**
* Set custom tabIndex attribute.
*/
tabIndex?: number;

/**
* Activate RTL support by passing `'rtl'`.
* You will also need a css rule with `direction: rtl`.
Expand Down
3 changes: 2 additions & 1 deletion packages/simplebar-vue/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ describe('simplebar', () => {

it('works with options as data attributes', () => {
const wrapper = shallowMount(simplebar, {
attrs: { 'data-simplebar-force-visible': 'true' },
attrs: { 'data-simplebar-force-visible': 'true', 'tabIndex': -1 },
});
expect(wrapper.vm.SimpleBar.options.forceVisible).toEqual(true);
expect(wrapper.vm.SimpleBar.options.tabIndex).toEqual(-1);
});

it('works with options as props', () => {
Expand Down
10 changes: 10 additions & 0 deletions packages/simplebar/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,16 @@ Controls the min and max size of the scrollbar in `px`.
Default for `scrollbarMinSize` is `25`.
Default for `scrollbarMaxSize` is `0` (no max size).

#### ariaLabel

Set custom aria-label attribute for users with screen reader.

The default value is `scrollable content`.

#### tabIndex

tabIndex to set for simplebar. Defaults to `0`.

### Apply scroll vertically only

Simply define in css `overflow-x: hidden` on your element.
Expand Down
2 changes: 1 addition & 1 deletion packages/simplebar/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export default class SimpleBar extends SimpleBarCore {
this.wrapperEl.appendChild(this.placeholderEl);
this.el.appendChild(this.wrapperEl);

this.contentWrapperEl?.setAttribute('tabindex', '0');
this.contentWrapperEl?.setAttribute('tabindex', this.options.tabIndex.toString());
this.contentWrapperEl?.setAttribute('role', 'region');
this.contentWrapperEl?.setAttribute('aria-label', this.options.ariaLabel);
}
Expand Down