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

feature: Allow external links to be a button element #32

Merged
merged 1 commit into from
Jun 20, 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
5 changes: 3 additions & 2 deletions src/Collapsable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,11 @@ export class Collapsable {
return
}

const extLinks = document.querySelectorAll<HTMLAnchorElement>(options.externalLinks.selector)
const extLinks = document.querySelectorAll<HTMLAnchorElement | HTMLButtonElement>(options.externalLinks.selector)

extLinks.forEach((element) => {
const collapsableItem = this.getItemById(element.hash.substring(1))
const hash = element instanceof HTMLAnchorElement ? element.hash.substring(1) : element.dataset.collapsableId
const collapsableItem = this.getItemById(hash)

if (!collapsableItem) {
return
Expand Down
18 changes: 11 additions & 7 deletions src/CollapsableExtLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@ import { Collapsable } from './Collapsable'
export class CollapsableExtLink {
private readonly collapsable: Collapsable

public readonly extLink: HTMLAnchorElement
public readonly extLink: HTMLAnchorElement | HTMLButtonElement
public readonly collapsableItem: CollapsableItem

private listener: EventListener | undefined
private ariaPerRole!: 'aria-expanded' | 'aria-selected' // always set by `this.prepareDOM()` called from constructor

public constructor(collapsable: Collapsable, link: HTMLAnchorElement, collapsableItem: CollapsableItem) {
public constructor(
collapsable: Collapsable,
link: HTMLAnchorElement | HTMLButtonElement,
collapsableItem: CollapsableItem
) {
this.collapsable = collapsable

if (!(link instanceof HTMLAnchorElement)) {
throw new Error('Collapsable: External link has to be HTMLAnchorElement.')
if (!(link instanceof HTMLAnchorElement) && !(link instanceof HTMLButtonElement)) {
throw new Error('Collapsable: External link has to be HTMLAnchorElement or HTMLButtonElement.')
}

this.extLink = link
Expand All @@ -27,11 +31,11 @@ export class CollapsableExtLink {
private prepareDOM(): void {
this.extLink.setAttribute('aria-controls', this.collapsableItem.boxElements.map((box) => box.id).join(' '))

if (!this.extLink.role) {
if (this.extLink instanceof HTMLAnchorElement && !this.extLink.role) {
this.extLink.role = 'button'
}

if (this.extLink.role === 'button') {
if (this.extLink instanceof HTMLButtonElement || this.extLink.role === 'button') {
this.ariaPerRole = 'aria-expanded'
} else if (this.extLink.role === 'tab') {
this.ariaPerRole = 'aria-selected'
Expand All @@ -44,7 +48,7 @@ export class CollapsableExtLink {
const { options } = this.collapsable

this.listener = (event: Event) => {
if (options.externalLinks.preventDefault) {
if (options.externalLinks.preventDefault || this.extLink.dataset.collapsablePreventDefault !== undefined) {
event.preventDefault()
}

Expand Down
Loading