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

feat: add option to set element class on hover #1212

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions src/event/behavior/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import './click'
import './mouseenter'
import './mouseleave'
import './cut'
import './keydown'
import './keypress'
Expand Down
7 changes: 7 additions & 0 deletions src/event/behavior/mouseenter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {behavior} from './registry'

behavior.mouseenter = (_, target, instance) => {
const className = instance.config.hoverClass
if (className == null) return
target.classList.add(className)
}
7 changes: 7 additions & 0 deletions src/event/behavior/mouseleave.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import {behavior} from './registry'

behavior.mouseleave = (_, target, instance) => {
const className = instance.config.hoverClass
if (className == null) return
target.classList.remove(className)
}
11 changes: 11 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,17 @@ export interface Options {
*/
skipHover?: boolean

/**
* The `:hover` pseudo-class cannot be applied programmatically.
*
* Instead, you can choose a CSS class name to be applied to elements when they are hovered.
* This requires the CSS to be written/transformed such that hover styles
* are applied with this custom class.
*
* @default null
*/
hoverClass?: string | null

/**
* Write selected data to Clipboard API when a `cut` or `copy` is triggered.
*
Expand Down
1 change: 1 addition & 0 deletions src/setup/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const defaultOptionsDirect: Required<Options> = {
skipAutoClose: false,
skipClick: false,
skipHover: false,
hoverClass: null,
writeToClipboard: false,
advanceTimers: () => Promise.resolve(),
}
Expand Down
2 changes: 1 addition & 1 deletion tests/setup/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {getSpy} from './_mockApis'
import {getConfig} from '@testing-library/dom'
import {getSpy} from './_mockApis'
import userEvent from '#src'
import {type Instance, type UserEventApi} from '#src/setup/setup'
import {render} from '#testHelpers'
Expand Down