Skip to content

Instantly share code, notes, and snippets.

@artzub
Last active January 10, 2021 06:12
Show Gist options
  • Select an option

  • Save artzub/10c13fd53a54888b0374f28e94408f82 to your computer and use it in GitHub Desktop.

Select an option

Save artzub/10c13fd53a54888b0374f28e94408f82 to your computer and use it in GitHub Desktop.

Revisions

  1. artzub revised this gist Jan 10, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion part.js
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@ const onFocus = (event) => {
    && current !== event.target
    ) {
    // skip anchors and areas without href and disabled items
    if (!checkHref(event.target) || element.disabled) {
    if (!checkHref(event.target) || event.target.disabled) {
    return;
    }
    current = event.target;
  2. artzub revised this gist Jan 10, 2021. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions part.js
    Original file line number Diff line number Diff line change
    @@ -7,8 +7,8 @@ const onFocus = (event) => {
    event?.target?.tabIndex > -1
    && current !== event.target
    ) {
    // skip anchors and area without href
    if (!checkHref(event.target)) {
    // skip anchors and areas without href and disabled items
    if (!checkHref(event.target) || element.disabled) {
    return;
    }
    current = event.target;
  3. artzub created this gist Jan 10, 2021.
    38 changes: 38 additions & 0 deletions part.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    const checkHref = (element) => {
    const tag = element.tagName;
    return !(tag === 'A' || tag === 'AREA') || element.href;
    };
    const onFocus = (event) => {
    if (
    event?.target?.tabIndex > -1
    && current !== event.target
    ) {
    // skip anchors and area without href
    if (!checkHref(event.target)) {
    return;
    }
    current = event.target;
    show(current);
    }
    };
    const onBlur = (event) => {
    if (event?.target?.tabIndex > -1) {
    // find parent item that can be focusable
    current = current?.parentNode;
    while (current && current.tabIndex < 0) {
    current = current.parentNode;
    }
    if (current === document) {
    current = null;
    }
    // return focus to activeElement
    if (!current && document.activeElement && document.activeElement !== document.body) {
    current = document.activeElement;
    }
    if (current) {
    show(current);
    } else {
    hide();
    }
    }
    };