Skip to content

Commit

Permalink
refactor: 💡 find footnote reference logic
Browse files Browse the repository at this point in the history
  • Loading branch information
lrodrigues-newstore committed Oct 6, 2024
1 parent 33fa0ee commit e4c8d5e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 21 deletions.
22 changes: 10 additions & 12 deletions src/dom/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,16 @@ function findReference<E extends Element>(
const processed: E[] = []
return (link: HTMLAnchorElement): [string, Element, E] | undefined => {
const fragment = link.href.split('#')[1]
const related = fragment
? queryAll<E>(document, '#' + window.CSS.escape(fragment)).find(
(footnote) => allowDuplicates || !processed.includes(footnote),
)
: undefined
const body = related?.closest<E>(footnoteSelector)

if (body) {
processed.push(body)
const reference = link.closest<E>(anchorParentSelector) || link
return [reference.id || link.id, reference, body]
}
if (!fragment) return

const body = queryAll<E>(document, '#' + window.CSS.escape(fragment))
.find((footnote) => allowDuplicates || !processed.includes(footnote))
?.closest<E>(footnoteSelector)
if (!body) return

processed.push(body)
const reference = link.closest<E>(anchorParentSelector) || link
return [reference.id || link.id, reference, body]
}
}

Expand Down
13 changes: 4 additions & 9 deletions src/dom/footnote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ export type FootnoteElements = Readonly<{
wrapper: HTMLElement
}>

const isMounted = (popover: HTMLElement) => document.body.contains(popover)

export function footnoteActions({
id,
button,
Expand All @@ -34,19 +32,18 @@ export function footnoteActions({
let maxHeight = 0
let position: Position = 'above'

const isMounted = () => document.body.contains(popover)

return {
id,

activate: (onActivate) => {
button.setAttribute('aria-expanded', 'true')
addClass(button, CLASS_CHANGING)
addClass(button, CLASS_ACTIVE)

button.insertAdjacentElement('afterend', popover)

popover.style.maxWidth = document.body.clientWidth + 'px'
maxHeight = getMaxHeight(content)

onActivate?.(popover, button)
},

Expand All @@ -55,7 +52,6 @@ export function footnoteActions({
addClass(button, CLASS_CHANGING)
removeClass(button, CLASS_ACTIVE)
removeClass(popover, CLASS_ACTIVE)

onDismiss?.(popover, button)
},

Expand All @@ -74,9 +70,8 @@ export function footnoteActions({
},

reposition: () => {
if (isMounted(popover)) {
if (isMounted()) {
const [next, height] = repositionPopover(popover, button, position)

position = next
content.style.maxHeight = Math.min(maxHeight, height) + 'px'

Expand All @@ -91,7 +86,7 @@ export function footnoteActions({
},

resize: () => {
if (isMounted(popover)) {
if (isMounted()) {
popover.style.left = getLeftInPixels(content, button) + 'px'
wrapper.style.maxWidth = content.offsetWidth + 'px'
repositionTooltip(popover, button)
Expand Down

0 comments on commit e4c8d5e

Please sign in to comment.