2 min read

BaseModal

BaseModal

Base component with no XPlugin dependencies that serves as a utility for constructing more complex modals.

Props

Name Description Type Default
animation Animation to use for opening/closing the modal. This animation only affects the content. union () => NoElement
overlayAnimation Animation to use for the overlay (backdrop) part of the modal. By default, it uses
a fade transition.
union () => Fade
open Determines if the modal is open or not. boolean
focusOnOpen Determines if the focused element changes to one inside the modal when it opens. Either the
first element with a positive tabindex or just the first focusable element.
boolean true
referenceSelector The reference selector of a DOM element to use as reference to position the modal.
This selector can be an ID or a class, if it is a class, it will use the first
element that matches.
string

Events

Event name Properties Description
click:overlay
focusin:body

Slots

Name Description Bindings
(name - type - description)
default (Required) Modal container content None

Examples

The BaseModal is a simple component that serves to create complex modals. Its open state has to be passed via prop. There is a prop, referenceSelector, used to place the modal under some element instead of set the top of the element directly. It also accepts an animation to use for opening & closing.

It emits a click:overlay event when any part out of the content is clicked, but only if the modal is open.

<template>
  <div>
    <button @click="open = true">Open modal</button>
    <BaseModal
      :animation="fadeAndSlide"
      :open="open"
      @click:overlay="open = false"
      referenceSelector=".header"
    >
      <h1>Hello</h1>
      <p>The modal is working</p>
      <button @click="open = false">Close modal</button>
    </BaseModal>
  </div>
</template>
<script>
  import { BaseModal, FadeAndSlide } from '@empathyco/x-components';
  import Vue from 'vue';
  Vue.component('fadeAndSlide', FadeAndSlide);
  export default {
    components: {
      BaseModal
    },
    data() {
      return {
        open: false
      };
    }
  };
</script>

Customized usage

Customizing the content with classes

The contentClass prop can be used to add classes to the modal content.

<template>
  <div>
    <button @click="open = true">Open modal</button>
    <BaseModal
      :animation="fadeAndSlide"
      :open="open"
      @click:overlay="open = false"
      referenceSelector=".header"
      contentClass="x-bg-neutral-75"
    >
      <h1>Hello</h1>
      <p>The modal is working</p>
      <button @click="open = false">Close modal</button>
    </BaseModal>
  </div>
</template>
<script>
  import { BaseModal, FadeAndSlide } from '@empathyco/x-components';
  import Vue from 'vue';
  Vue.component('fadeAndSlide', FadeAndSlide);
  export default {
    components: {
      BaseModal
    },
    data() {
      return {
        open: false
      };
    }
  };
</script>

Customizing the overlay with classes

The overlayClass prop can be used to add classes to the modal overlay.

<template>
  <div>
    <button @click="open = true">Open modal</button>
    <BaseModal
      :animation="fadeAndSlide"
      :open="open"
      @click:overlay="open = false"
      referenceSelector=".header"
      overlayClass="x-bg-neutral-75"
    >
      <h1>Hello</h1>
      <p>The modal is working</p>
      <button @click="open = false">Close modal</button>
    </BaseModal>
  </div>
</template>
<script>
  import { BaseModal, FadeAndSlide } from '@empathyco/x-components';
  import Vue from 'vue';
  Vue.component('fadeAndSlide', FadeAndSlide);
  export default {
    components: {
      BaseModal
    },
    data() {
      return {
        open: false
      };
    }
  };
</script>

Vue Events

A list of events that the component will emit:

  • click:overlay: the event is emitted after the user clicks any part out of the content but only if the modal is open. The event payload is the mouse event that triggers it.
  • focusin:body: the event is emitted after the user focus in any part out of the content but only if the modal is open. The event payload is the focus event that triggers it.