vue3-popper

repository·main·Indexed 42 days ago

https://github.com/valgeirb/vue3-popper

A Vue 3 component library for creating and managing popover elements using PopperJS v2. It supports various trigger modes, customizable styling via CSS variables, and provides a flexible API with props for placement, delays, and manual visibility control. Version 1.4.2.

Tokens
4.8K
Snippets
15
Records
21
Agent score
54%

What's inside vue3-popper

  1. Use the Popper component in Vue 3

    main

    To use Popper, import it from vue3-popper and register it in your component. You can provide content in two ways:

    1. Simple String: Use the content prop for plain text.
    2. Complex Content: Use the #content slot for HTML or complex Vue components.

    Note that the Popper component wraps a trigger element (like a button) which acts as the anchor for the popover.

    <!-- Simple string content using the content prop -->
    <template>
      <Popper content="This is the Popper content">
        <button>Trigger element</button>
      </Popper>
    </template>
    
    <!-- Complex content using the #content slot -->
    <template>
      <Popper>
        <button>Trigger element</button>
        <template #content>
          <div>This is the Popper content</div>
        </template>
      </Popper>
    </template>
    
    <script>
      import { defineComponent } from "vue";
      import Popper from "vue3-popper";
    
      export default defineComponent({
        components: {
          Popper,
        },
      });
    </script>
  2. Use the content prop or slot for Popper content

    main

    To define what appears inside the popover, you can either use the content prop for simple strings or the #content slot for complex HTML/components.

    <!-- Using the content prop for simple strings -->
    <template>
      <Popper content="This is the Popper content">
        <button>Trigger element</button>
      </Popper>
    </template>
    <!-- Using the #content slot for complex content -->
    <template>
      <Popper>
        <button>Trigger element</button>
        <template #content>
          <div>This is the Popper content</div>
        </template>
      </Popper>
    </template>
  3. Register Popper globally or locally

    main

    You can register the Popper component globally in your Vue application or import it locally for specific components.

    // Global registration
    import { createApp } from "vue";
    import Popper from "vue3-popper";
    
    const app = createApp({});
    app.component("Popper", Popper);
    <!-- Local registration -->
    <template>
      <Popper content="This is the Popper content 🍿">
        <button>Trigger element</button>
      </Popper>
    </template>
    
    <script>
      import { defineComponent } from "vue";
      import Popper from "vue3-popper";
    
      export default defineComponent({
        components: {
          Popper,
        },
      });
    </script>
  4. Quick Start with vue3-popper

    main
    To add popovers to your Vue 3 project, follow the getting started guide to install the package and integrate it into your application. vue3-popper is written in Vue 3 and utilizes the latest version of PopperJS to provide a simple, themable API for managing popover positioning and behavior.
  5. Wrap Popper in a custom component

    main

    To improve modularity and scalability, you can wrap Popper in your own component. This allows you to hardcode certain behaviors (like hover or openDelay) and provide a single point of change for your application.

    <template>
      <!-- Hardcoded "hover", "openDelay" and "closeDelay" -->
      <Popper v-bind="$attrs" hover openDelay="200" closeDelay="100">
        <slot />
        <template #content="props">
          <slot name="content" v-bind="props" />
        </template>
      </Popper>
    </template>
    
    <script>
      import { defineComponent } from "vue";
      import Popper from "vue3-popper";
    
      export default defineComponent({
        name: "MyPopperWrapper",
        components: {
          Popper,
        },
      });
    </script>
  6. Apply custom styles using :deep selector

    main

    If you prefer not to use CSS variables, you can apply custom styles directly to the .popper class. When using <style scoped>, you must use the :deep() selector to target the Popper's internal elements.

    <style scoped>
      :deep(.popper) {
        background: #e92791;
        padding: 20px;
        border-radius: 20px;
        color: #fff;
        font-weight: bold;
        text-transform: uppercase;
      }
    
      :deep(.popper #arrow::before) {
        background: #e92791;
      }
    
      :deep(.popper:hover),
      :deep(.popper:hover > #arrow::before) {
        background: #e92791;
      }
    </style>
  7. Customize Popper styles using CSS variables

    main

    The Popper component uses a set of predefined CSS variables that you can overwrite to change its appearance. You can apply these globally via :root or locally within a component.

    :root {
      --popper-theme-background-color: #333333;
      --popper-theme-background-color-hover: #333333;
      --popper-theme-text-color: #ffffff;
      --popper-theme-border-width: 0px;
      --popper-theme-border-style: solid;
      --popper-theme-border-radius: 6px;
      --popper-theme-padding: 32px;
      --popper-theme-box-shadow: 0 6px 30px -6px rgba(0, 0, 0, 0.25);
    }
  8. React to Popper events

    main

    You can listen to built-in events to trigger side-effects when the Popper opens or closes using @open:popper and @close:popper.

    <template>
      <Popper
        arrow
        @open:popper="openAlert"
        @close:popper="closeAlert"
        content="This is the Popper content 🍿"
      >
        <Button>Demo</Button>
      </Popper>
    </template>
    
    <script>
      import { defineComponent } from "vue";
      export default defineComponent({
        name: "PopperEvents",
        methods: {
          openAlert() {
            alert("Opening Popper!");
          },
          closeAlert() {
            alert("Closing Popper!");
          },
        },
      });
    </script>