Skip to content

Commit

Permalink
feat: use popover instead of <dialog>
Browse files Browse the repository at this point in the history
  • Loading branch information
florianstancioiu committed Apr 9, 2024
1 parent cb6c629 commit 6620f87
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions src/cosmoz-dropdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,18 @@ interface DropdownProps
Pick<ContentProps, 'placement' | 'render'> {}

const Dropdown = (host: HTMLElement & DropdownProps) => {
const dialogChanged = (dialog?: HTMLDialogElement) => {
const supportsPopover = () => {
// eslint-disable-next-line no-prototype-builtins
return HTMLElement.prototype.hasOwnProperty('popover');
};

const popoverChanged = (popover?: Element) => {
const popoverElement = popover as HTMLElement;

requestAnimationFrame(() => {
dialog?.showModal();
if (supportsPopover()) {
popoverElement?.showPopover();
}
});
};

Expand Down Expand Up @@ -98,9 +107,12 @@ const Dropdown = (host: HTMLElement & DropdownProps) => {
left: auto;
}
}
#content {
position: absolute;
#content:popover-open {
background-color: transparent;
width: 0;
height: 0;
inset: none;
border: none;
}
</style>
<div class="anchor" part="anchor">
Expand All @@ -116,7 +128,7 @@ const Dropdown = (host: HTMLElement & DropdownProps) => {
${when(
active,
() =>
html`<dialog ${ref(dialogChanged)} id="content">
html`<div ${ref(popoverChanged)} popover id="content">
<cosmoz-dropdown-content
part="content"
exportparts="wrap, content"
Expand All @@ -126,7 +138,7 @@ const Dropdown = (host: HTMLElement & DropdownProps) => {
>
<slot></slot>
</cosmoz-dropdown-content>
</dialog>`,
</div>`,
)}`;
};

Expand Down

0 comments on commit 6620f87

Please # to comment.