-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Laura Bergoens <laura.bergoens@pix.fr> Co-authored-by: Jérémie Jadé <jeremie.jade@pix.fr>
- Loading branch information
1 parent
8389e0d
commit 5e21eff
Showing
4 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { action } from '@ember/object'; | ||
import { service } from '@ember/service'; | ||
import { tracked } from '@glimmer/tracking'; | ||
import { on } from '@ember/modifier'; | ||
import { next } from '@ember/runloop'; | ||
import Component from '@glimmer/component'; | ||
import PixIconButton from "@1024pix/pix-ui/components/pix-icon-button"; | ||
|
||
export default class DropdownMenu extends Component { | ||
@service router; | ||
|
||
@tracked isMenuDisplayed = false; | ||
randomMenuId = `menu-id-${window.crypto.randomUUID()}`; | ||
randomButtonId = `button-id-${window.crypto.randomUUID()}`; | ||
|
||
getOverflowHiddenParent(node) { | ||
const parentNode = node.parent; | ||
if(parentNode.style.overflow === 'hidden') { | ||
return parentNode; | ||
} | ||
return this.getOverflowHiddenParent(parentNode); | ||
} | ||
|
||
displayMenuManager() { | ||
const menu = document.querySelector(`#${this.randomMenuId}`); | ||
const menuRect = menu.getBoundingClientRect(); | ||
const parent = this.getOverflowHiddenParent(menu) | ||
if(parent) { | ||
// todo: vérifier la position par rapport a window | ||
// si pas d'overlape avec la window -> verifier par rapport au parent qui a overflow hidden | ||
} | ||
if(menuRect.bottom > window.innerHeight) { | ||
menu.style.bottom = '100%'; | ||
menu.style.top = null | ||
} else { | ||
menu.style.top = '100%'; | ||
menu.style.bottom = null | ||
} | ||
menu.focus() | ||
} | ||
|
||
@action | ||
onTrigger() { | ||
this.isMenuDisplayed = !this.isMenuDisplayed; | ||
if(this.isMenuDisplayed) { | ||
next(this, this.displayMenuManager) | ||
} | ||
} | ||
|
||
@action | ||
onHide(event) { | ||
if (document.querySelector(`#${this.randomMenuId}`).contains(event.relatedTarget)) return; | ||
this.isMenuDisplayed = false; | ||
} | ||
|
||
|
||
<template> | ||
<div class="dropdown"> | ||
<PixIconButton | ||
id={{this.randomButtonId}} | ||
@ariaLabel={{@ariaLabel}} | ||
@iconName={{@iconName}} | ||
@plainIcon={{@plainIcon}} | ||
@triggerAction={{this.onTrigger}} | ||
@withBackground={{@withBackground}} | ||
@size={{@size}} | ||
aria-haspopup="true" | ||
aria-controls={{this.randomMenuId}} | ||
aria-expanded="{{this.isMenuDisplayed}}" | ||
/> | ||
<ul | ||
id={{this.randomMenuId}} | ||
tabindex="-1" | ||
class="dropdown-menu{{if this.isMenuDisplayed "" "--hidden"}}" | ||
{{on 'focusout' this.onHide}} | ||
aria-labelledby={{this.randomButtonId}} | ||
role="menu" | ||
> | ||
{{yield}} | ||
</ul> | ||
</div> | ||
</template> | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
.dropdown { | ||
position: relative; | ||
display: inline-block; | ||
|
||
&-menu { | ||
position: absolute; | ||
right: 0; | ||
top: 100%; | ||
z-index: 10; | ||
background: var(--pix-neutral-20); | ||
border-radius: var(--pix-spacing-2x); | ||
padding: var(--pix-spacing-2x); | ||
|
||
&--hidden { | ||
display: none; | ||
} | ||
} | ||
} |