To render a popover, add your HTML code to a data-popover-content
attribute. Then,
you can initialize it with the chi.popover
factory method.
<button id='popover-1' class="a-btn" data-popover-content='<header class="m-popover__header"><h2 class="m-popover__title">Popover title</h2></header><div class="m-popover__content"><p class="m-popover__text">Popover content.</p></div>'>Popover</button>
<script>chi.popover(document.getElementById('popover-1'));</script>
You can also link the popover via data-target
attribute.
TipDon't add the arrow to the Popover template. It is configured via the arrow
configuration parameter.
TipIf you specify the content via the data-content
attribute or the content
configuration
parameter, it will override the contents of the target. Use it wisely.
<button class="a-btn" id="popover-1b" data-target="#popover-in-html">Popover</button>
<!-- put the following code anywere in the DOM, e.g. at the end of the DOM -->
<section class="m-popover" id="popover-in-html" aria-modal="true" role="dialog" aria-label="Popover title">
<header class="m-popover__header">
<h2 class="m-popover__title">Popover title</h2>
</header>
<div class="m-popover__content">
<p class="m-popover__text">Popover content.</p>
</div>
</section>
<script>chi.popover(document.getElementById('popover-1b'));</script>
By default, popover's position on top of an element. To alter position, use
the data-position
attribute. Valid values are top
,
right
, bottom
, left
, top-start
,
top-end
, right-start
, right-end
,
bottom-start
, bottom-end
, left-start
,
left-end
.
<button data-position='top' data-popover-content='<header class="m-popover__header"><h2 class="m-popover__title">Popover positioning</h2></header><div class="m-popover__content"><p class="m-popover__text">This popover is top positioned.</p></div>' class="a-btn">Top popover</button>
<button data-position='top-start' data-popover-content='<header class="m-popover__header"><h2 class="m-popover__title">Popover positioning</h2></header><div class="m-popover__content"><p class="m-popover__text">This popover is top start positioned.</p></div>'>Top start popover</button>
<button data-position='top-end' data-popover-content='<header class="m-popover__header"><h2 class="m-popover__title">Popover positioning</h2></header><div class="m-popover__content"><p class="m-popover__text">This popover is top end positioned.</p></div>'>Top end popover</button>
<script>chi.popover(document.querySelectorAll('[data-popover-content])');</script>
<button data-position='right' data-popover-content='<header class="m-popover__header"><h2 class="m-popover__title">Popover positioning</h2></header><div class="m-popover__content"><p class="m-popover__text">This popover is right positioned.</p></div>'>Right popover</button>
<button data-position='right-start' data-popover-content='<header class="m-popover__header"><h2 class="m-popover__title">Popover positioning</h2></header><div class="m-popover__content"><p class="m-popover__text">This popover is right start positioned.</p></div>'>Right start popover</button>
<button data-position='right-end' data-popover-content='<header class="m-popover__header"><h2 class="m-popover__title">Popover positioning</h2></header><div class="m-popover__content"><p class="m-popover__text">This popover is right end positioned.</p></div>'>Right end popover</button>
<script>chi.popover(document.querySelectorAll('[data-popover-content])');</script>
<button data-position='bottom' data-popover-content='<header class="m-popover__header"><h2 class="m-popover__title">Popover positioning</h2></header><div class="m-popover__content"><p class="m-popover__text">This popover is bottom positioned.</p></div>'>Bottom popover</button>
<button data-position='bottom-start' data-popover-content='<header class="m-popover__header"><h2 class="m-popover__title">Popover positioning</h2></header><div class="m-popover__content"><p class="m-popover__text">This popover is bottom start positioned.</p></div>'>Bottom start popover</button>
<button data-position='bottom-end' data-popover-content='<header class="m-popover__header"><h2 class="m-popover__title">Popover positioning</h2></header><div class="m-popover__content"><p class="m-popover__text">This popover is bottom end positioned.</p></div>'>Bottom end popover</button>
<script>chi.popover(document.querySelectorAll('[data-popover-content])');</script>
<button data-position='left' data-popover-content='<header class="m-popover__header"><h2 class="m-popover__title">Popover positioning</h2></header><div class="m-popover__content"><p class="m-popover__text">This popover is left positioned.</p></div>'>Left popover</button>
<button data-position='left-start' data-popover-content='<header class="m-popover__header"><h2 class="m-popover__title">Popover positioning</h2></header><div class="m-popover__content"><p class="m-popover__text">This popover is left start positioned.</p></div>'>Left start popover</button>
<button data-position='left-end' data-popover-content='<header class="m-popover__header"><h2 class="m-popover__title">Popover positioning</h2></header><div class="m-popover__content"><p class="m-popover__text">This popover is left end positioned.</p></div>'>Left end popover</button>
<script>chi.popover(document.querySelectorAll('[data-popover-content])');</script>
This component accepts options to configure its behavior.
Use this method to change the content of the popover. It discards any previously attached content, so any resources attached to the content should be removed to prevent memory leaks. This method accepts a string or an HTML DOM Element as a parameter.
<button id='popover-2' class="a-btn" data-popover-content='<div class="m-popover__content">Foo</div>'>Popover</button>
<script>
var popover = chi.popover(document.getElementById('popover-2'));
popover.setContent('<div class="m-popover__content">Bar</div>');
</script>
Use this methods to control popover visibility.
<button id='popover-3' class="a-btn" data-popover-content='<div class="m-popover__content">Foo</div>'>Popover</button>
<button id='show-popover-3' class="a-btn">Show</button>
<button id='hide-popover-3' class="a-btn">Hide</button>
<button id='toggle-popover-3' class="a-btn">Toggle</button>
<script>
var popover = chi.popover(
document.getElementById('popover-3'),
{
trigger: 'manual',
preventAutoHide: true
}
);
document.getElementById('show-popover-3').addEventListener('click', function(e) {
popover.show()
});
document.getElementById('hide-popover-3').addEventListener('click', function(e) {
popover.hide()
});
document.getElementById('toggle-popover-3').addEventListener('click', function(e) {
popover.toggle()
});
</script>
The element the popover is attached to (usually a button), dispatches this event when the popover is to be shown.
The element the popover is attached to (usually a button), dispatches this event when the popover is to be hidden.
Popover components have a dispose function to free all resources attached to the element, such as event listeners and object data. You must call this method when you want to remove the component.
var elem = document.getElementById('popover');
var popover = chi.popover(elem);
// do stuff
popover.dispose();
TipIt is safe to call the popover
method more than once, as it will return any previously created popover component
associated to the trigger.
var elem = document.getElementById('popover-1');
var popover = chi.popover(elem);
var elem2 = document.getElementById('popover-1');
var popover2 = chi.popover(elem2);
popover === popover2; // returns true
popover.dispose(); // Only have to do it once.
Popover content at the end of the DOM