Skip to content

Commit

Permalink
Merge pull request #280 from codex-team/fix-popover-toggle
Browse files Browse the repository at this point in the history
fix(ui): popover toggle fix
  • Loading branch information
neSpecc authored Dec 9, 2024
2 parents f8878f9 + c7b5d50 commit 9e74cb2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
4 changes: 2 additions & 2 deletions codex-ui/dev/pages/components/Popover.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<div>
<Button
secondary
@click="show($event.target, {vertically: 'below', horizontally: 'left'})"
@click="!isOpen ? show($event.target, {vertically: 'below', horizontally: 'left'}) : hide()"
>
Open below left
</Button>
Expand Down Expand Up @@ -82,7 +82,7 @@
import PageHeader from '../../components/PageHeader.vue';
import { usePopover, PopoverShowParams, Button, ContextMenu, Heading } from '../../../src/vue';
const { showPopover } = usePopover();
const { showPopover, isOpen, hide } = usePopover();
/**
* Example of working with Popover
Expand Down
8 changes: 7 additions & 1 deletion codex-ui/src/vue/components/popover/Popover.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,18 @@ const {
hide,
content,
width,
targetElement,
} = usePopover();
/**
* Close the popover when clicking outside of it
*/
onClickOutside(popoverEl, hide);
onClickOutside(popoverEl, hide, {
/**
* Allow clicks on the target element to implemet toggle behavior
*/
ignore: [targetElement],
});
</script>

<style module>
Expand Down
8 changes: 8 additions & 0 deletions codex-ui/src/vue/components/popover/usePopover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ export const usePopover = createSharedComposable(() => {
*/
const content = shallowRef<PopoverContent | null>(null);

/**
* Target element to move popover to
*/
const targetElement = ref<HTMLElement | null>(null);

/**
* Move popover to the target element
* Also, align and set width
Expand Down Expand Up @@ -128,6 +133,7 @@ export const usePopover = createSharedComposable(() => {
* @param params - popover showing configuration
*/
function showPopover(params: PopoverShowParams): void {
targetElement.value = params.targetEl;
move(params.targetEl, params.align, params.width);
mountComponent(params.with.component, params.with.props);
show();
Expand All @@ -137,6 +143,7 @@ export const usePopover = createSharedComposable(() => {
* Empty content, position and hide popover
*/
function resetPopover(): void {
targetElement.value = null;
content.value = null;
position.left = 0;
position.top = 0;
Expand All @@ -159,5 +166,6 @@ export const usePopover = createSharedComposable(() => {
hide,
content,
width,
targetElement,
};
});

0 comments on commit 9e74cb2

Please sign in to comment.