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

Feature/add strategy prop #83

Open
wants to merge 3 commits 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 18 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,24 @@ yarn add vue3-popper

## Props

| Name | Default | Description |
| ------------------ | -------- | ----------------------------------------------------------------------------------------------------------- |
| `placement` | `bottom` | Preferred placement of the Popper |
| `disableClickAway` | `false` | Disables automatically closing the Popper when the user clicks away from it |
| `offsetSkid` | `0` | Offset in pixels along the trigger element |
| `offsetDistance` | `12` | Offset in pixels away from the trigger element |
| `hover` | `false` | Trigger the Popper on hover |
| `arrow` | `false` | Display an arrow on the Popper |
| `arrowPadding` | `0` | Stop arrow from reaching the edge of the Popper (in pixels) |
| `disabled` | `false` | Disables the Popper. If it was already open, it will be closed. |
| `openDelay` | `0` | Open the Popper after a delay (ms) |
| `closeDelay` | `0` | Close the Popper after a delay (ms) |
| `interactive` | `true` | If the Popper should be interactive, it will close when clicked/hovered if false |
| `content` | `null` | If your content is just a simple string, you can pass it as a prop |
| `show` | `null` | Control the Popper **manually**, other events (click, hover) are ignored if this is set to `true/false` |
| `zIndex` | `9999` | The z-index of the Popper |
| `locked` | `false` | Lock the Popper into place, it will not flip dynamically when it runs out of space if this is set to `true` |
| Name | Default | Description |
| ------------------ | -----------| ------------------------------------------------------------------------------------------------------------- |
| `placement` | `bottom` | Preferred placement of the Popper |
| `disableClickAway` | `false` | Disables automatically closing the Popper when the user clicks away from it |
| `offsetSkid` | `0` | Offset in pixels along the trigger element |
| `offsetDistance` | `12` | Offset in pixels away from the trigger element |
| `hover` | `false` | Trigger the Popper on hover |
| `arrow` | `false` | Display an arrow on the Popper |
| `arrowPadding` | `0` | Stop arrow from reaching the edge of the Popper (in pixels) |
| `disabled` | `false` | Disables the Popper. If it was already open, it will be closed. |
| `openDelay` | `0` | Open the Popper after a delay (ms) |
| `closeDelay` | `0` | Close the Popper after a delay (ms) |
| `interactive` | `true` | If the Popper should be interactive, it will close when clicked/hovered if false |
| `content` | `null` | If your content is just a simple string, you can pass it as a prop |
| `show` | `null` | Control the Popper **manually**, other events (click, hover) are ignored if this is set to `true/false` |
| `zIndex` | `9999` | The z-index of the Popper |
| `locked` | `false` | Lock the Popper into place, it will not flip dynamically when it runs out of space if this is set to `true` |
| `strategy` | `absolute` | Positioning strategy, either `absolute` or `fixed` (see https://popper.js.org/docs/v2/constructors/#strategy) |

## Events

Expand Down
35 changes: 18 additions & 17 deletions docs/guide/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,24 @@

## Props

| Name | Default | Description |
| ------------------ | -------- | ----------------------------------------------------------------------------------------------------------- |
| `placement` | `bottom` | Preferred placement of the Popper |
| `disableClickAway` | `false` | Disables automatically closing the Popper when the user clicks away from it |
| `offsetSkid` | `0` | Offset in pixels along the trigger element |
| `offsetDistance` | `12` | Offset in pixels away from the trigger element |
| `hover` | `false` | Trigger the Popper on hover |
| `arrow` | `false` | Display an arrow on the Popper |
| `arrowPadding` | `0` | Stop arrow from reaching the edge of the Popper (in pixels) |
| `disabled` | `false` | Disables the Popper. If it was already open, it will be closed. |
| `openDelay` | `0` | Open the Popper after a delay (ms) |
| `closeDelay` | `0` | Close the Popper after a delay (ms) |
| `interactive` | `true` | If the Popper should be interactive, it will close when clicked/hovered if false |
| `content` | `null` | If your content is just a simple string, you can pass it as a prop |
| `show` | `null` | Control the Popper **manually**, other events (click, hover) are ignored if this is set to `true/false` |
| `zIndex` | `9999` | The z-index of the Popper |
| `locked` | `false` | Lock the Popper into place, it will not flip dynamically when it runs out of space if this is set to `true` |
| Name | Default | Description |
| ------------------ | -----------| ------------------------------------------------------------------------------------------------------------- |
| `placement` | `bottom` | Preferred placement of the Popper |
| `disableClickAway` | `false` | Disables automatically closing the Popper when the user clicks away from it |
| `offsetSkid` | `0` | Offset in pixels along the trigger element |
| `offsetDistance` | `12` | Offset in pixels away from the trigger element |
| `hover` | `false` | Trigger the Popper on hover |
| `arrow` | `false` | Display an arrow on the Popper |
| `arrowPadding` | `0` | Stop arrow from reaching the edge of the Popper (in pixels) |
| `disabled` | `false` | Disables the Popper. If it was already open, it will be closed. |
| `openDelay` | `0` | Open the Popper after a delay (ms) |
| `closeDelay` | `0` | Close the Popper after a delay (ms) |
| `interactive` | `true` | If the Popper should be interactive, it will close when clicked/hovered if false |
| `content` | `null` | If your content is just a simple string, you can pass it as a prop |
| `show` | `null` | Control the Popper **manually**, other events (click, hover) are ignored if this is set to `true/false` |
| `zIndex` | `9999` | The z-index of the Popper |
| `locked` | `false` | Lock the Popper into place, it will not flip dynamically when it runs out of space if this is set to `true` |
| `strategy` | `absolute` | Positioning strategy, either `absolute` or `fixed` (see https://popper.js.org/docs/v2/constructors/#strategy) |

## Events

Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ declare module "vue3-popper" {
interactive?: boolean;
locked?: boolean;
content?: string;
strategy?: "absolute" | "fixed";
"onOpen:popper"?: () => void;
"onClose:popper"?: () => void;
}
Expand Down
13 changes: 13 additions & 0 deletions src/component/Popper.vue
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,17 @@
type: String,
default: null,
},
/**
* Positioning strategy, defaults to absolute
* @see https://popper.js.org/docs/v2/constructors/#strategy
*/
strategy: {
type: String,
default: "absolute",
validator: function(value) {
return ["absolute", "fixed"].includes(value);
},
}
});

const popperContainerNode = ref(null);
Expand Down Expand Up @@ -203,6 +214,7 @@
openDelay,
placement,
show,
strategy,
} = toRefs(props);

const { isOpen, open, close } = usePopper({
Expand All @@ -214,6 +226,7 @@
placement,
popperNode,
triggerNode,
strategy,
});

const { hasContent } = useContent(slots, popperNode, content);
Expand Down
6 changes: 4 additions & 2 deletions src/composables/usePopper.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default function usePopper({
placement,
popperNode,
triggerNode,
strategy,
}) {
const state = reactive({
isOpen: false,
Expand Down Expand Up @@ -51,8 +52,8 @@ export default function usePopper({
emit("open:popper");
};

// When isOpen or placement change
watch([() => state.isOpen, placement], async ([isOpen]) => {
// When isOpen, placement or strategy change
watch([() => state.isOpen, placement, strategy], async ([isOpen]) => {
if (isOpen) {
await initializePopper();
enablePopperEventListeners();
Expand Down Expand Up @@ -87,6 +88,7 @@ export default function usePopper({
},
},
],
strategy: strategy.value,
});

// Update its position
Expand Down