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

Fix context menu position when opened via keyboard #8827

Merged
merged 2 commits into from
Apr 17, 2023
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
6 changes: 6 additions & 0 deletions changelog/unreleased/bugfix-context-menu-keyboard
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Bugfix: Opening context menu via keyboard

The position of the context menu when opened via keyboard has been fixed.

https://github.com/owncloud/web/pull/8827
https://github.com/owncloud/web/issues/8232
9 changes: 7 additions & 2 deletions packages/web-pkg/src/helpers/contextMenuDropdown.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
export const displayPositionedDropdown = (dropdown, event, contextMenuButton) => {
const contextMenuButtonPos = contextMenuButton.$el.getBoundingClientRect()
const isKeyboardEvent = event.clientY === 0
const yValue = isKeyboardEvent
? event.srcElement?.getBoundingClientRect().top || 0
: event.clientY

dropdown.setProps({
getReferenceClientRect: () => ({
width: 0,
height: 0,
top: event.clientY,
bottom: event.clientY,
top: yValue,
bottom: yValue,
/**
* If event type is 'contextmenu' the trigger was a right click on the table row,
* so we render the dropdown at the position of the mouse pointer.
Expand Down
33 changes: 32 additions & 1 deletion packages/web-pkg/tests/unit/helpers/contextMenuDropdown.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ describe('displayPositionedDropdown', () => {
it('shows the dropdown', () => {
const dropdown = { setProps: jest.fn(), show: jest.fn() }
const ctxMenuBtn = { $el: { getBoundingClientRect: jest.fn() } }
displayPositionedDropdown(dropdown, undefined, ctxMenuBtn)
displayPositionedDropdown(dropdown, {}, ctxMenuBtn)
expect(dropdown.show).toHaveBeenCalled()
})
it('horizontally positions the drop at the cursor for "contextmenu"-event', () => {
Expand Down Expand Up @@ -38,4 +38,35 @@ describe('displayPositionedDropdown', () => {
expect(data.left).toEqual(ctxMenuBtnX)
expect(data.right).toEqual(ctxMenuBtnX)
})
it('vertically positions the drop via "clientY" if given', () => {
const event = { clientY: 100 }
let dropdownProps
const dropdown = {
setProps: jest.fn((props) => {
dropdownProps = props
}),
show: jest.fn()
}
const ctxMenuBtn = { $el: { getBoundingClientRect: jest.fn(() => ({ x: 200 })) } }
displayPositionedDropdown(dropdown, event, ctxMenuBtn)
const { top, bottom } = dropdownProps.getReferenceClientRect()
expect(top).toEqual(event.clientY)
expect(bottom).toEqual(event.clientY)
})
it('vertically positions the drop via the context button position if "clientY" is 0', () => {
const yPos = 200
const event = { clientY: 0, srcElement: { getBoundingClientRect: () => ({ top: yPos }) } }
let dropdownProps
const dropdown = {
setProps: jest.fn((props) => {
dropdownProps = props
}),
show: jest.fn()
}
const ctxMenuBtn = { $el: { getBoundingClientRect: jest.fn(() => ({ x: 200 })) } }
displayPositionedDropdown(dropdown, event, ctxMenuBtn)
const { top, bottom } = dropdownProps.getReferenceClientRect()
expect(top).toEqual(yPos)
expect(bottom).toEqual(yPos)
})
})