Skip to content

Commit

Permalink
fix: focus tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
astagi authored Dec 3, 2024
1 parent b076b91 commit 27d3714
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 113 deletions.
62 changes: 0 additions & 62 deletions .github/workflows/check-tree-shakeability.yml

This file was deleted.

8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@
"main": "./dist/index.cjs"
}
},
"sideEffects": false,
"sideEffects": [
"dist/track-focus.js",
"src/track-focus.js",
"dist/index.ts",
"dist/index.js",
"src/index.ts"
],
"files": [
"dist",
"src"
Expand Down
50 changes: 1 addition & 49 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,52 +299,4 @@ export type {
UncontrolledTooltipProps
} from 'reactstrap';

// Focus Management

/**
* --------------------------------------------------------------------------
* Bootstrap Italia (https://italia.github.io/bootstrap-italia/)
* Authors: https://github.com/italia/bootstrap-italia/blob/main/AUTHORS
* Licensed under BSD-3-Clause license (https://github.com/italia/bootstrap-italia/blob/main/LICENSE)
* --------------------------------------------------------------------------
*/

// const DATA_MOUSE_FOCUS = 'data-focus-mouse'
// const CLASS_NAME_MOUSE_FOCUS = 'focus--mouse'

// class TrackFocus {
// private _usingMouse: boolean;
// constructor() {
// this._usingMouse = false

// this._bindEvents()
// }

// _bindEvents() {
// if (typeof document === 'undefined') {
// return
// }
// const events = ['keydown', 'mousedown']
// events.forEach((evtName) => {
// document.addEventListener(evtName, (evt) => {
// this._usingMouse = evt.type === 'mousedown'
// })
// })
// document.addEventListener('focusin', (evt: Event) => {
// if (this._usingMouse) {
// if (evt.target) {
// (evt.target as HTMLElement).classList.add(CLASS_NAME_MOUSE_FOCUS);
// (evt.target as HTMLElement).setAttribute(DATA_MOUSE_FOCUS, 'true')
// }
// }
// })
// document.addEventListener('focusout', (evt: Event) => {
// if (evt.target) {
// (evt.target as HTMLElement).classList.remove(CLASS_NAME_MOUSE_FOCUS);
// (evt.target as HTMLElement).setAttribute(DATA_MOUSE_FOCUS, 'false')
// }
// })
// }
// }

// new TrackFocus()
import "./track-focus.js"
49 changes: 49 additions & 0 deletions src/track-focus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@

// Focus Management

/**
* --------------------------------------------------------------------------
* Bootstrap Italia (https://italia.github.io/bootstrap-italia/)
* Authors: https://github.com/italia/bootstrap-italia/blob/main/AUTHORS
* Licensed under BSD-3-Clause license (https://github.com/italia/bootstrap-italia/blob/main/LICENSE)
* --------------------------------------------------------------------------
*/

const DATA_MOUSE_FOCUS = 'data-focus-mouse'
const CLASS_NAME_MOUSE_FOCUS = 'focus--mouse'

class TrackFocus {
constructor() {
this._usingMouse = false

this._bindEvents()
}

_bindEvents() {
if (typeof document === 'undefined') {
return
}
const events = ['keydown', 'mousedown']
events.forEach((evtName) => {
document.addEventListener(evtName, (evt) => {
this._usingMouse = evt.type === 'mousedown'
})
})
document.addEventListener('focusin', (evt) => {
if (this._usingMouse) {
if (evt.target) {
evt.target.classList.add(CLASS_NAME_MOUSE_FOCUS);
evt.target.setAttribute(DATA_MOUSE_FOCUS, 'true')
}
}
})
document.addEventListener('focusout', (evt) => {
if (evt.target) {
evt.target.classList.remove(CLASS_NAME_MOUSE_FOCUS);
evt.target.setAttribute(DATA_MOUSE_FOCUS, 'false')
}
})
}
}

new TrackFocus()
2 changes: 1 addition & 1 deletion tsup.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defineConfig } from 'tsup';

export default defineConfig({
entry: ['src/**/*.@(ts|tsx)'],
entry: ['src/**/*.@(js|ts|tsx)'],
format: ['cjs', 'esm'],
target: 'esnext',
dts: false,
Expand Down

0 comments on commit 27d3714

Please sign in to comment.