Popover - 图1

Popover

Popover compontent is used to manage the presentation of content in a popover. You use popovers to present information temporarily. The popover remains visible until the user taps outside of the popover window or you explicitly dismiss it.

Note that is not recommended to use Popover on small screens (iPhone). On small screens you should use Action Sheet instead.

Popover Layout

First of all let’s look on Popover layout, it is pretty simple:

  1. <body>
  2. ...
  3. <div class="popover">
  4. <!-- Popover's angle arrow -->
  5. <div class="popover-angle"></div>
  6. <!-- Popover content -->
  7. <div class="popover-inner">
  8. <!-- Any HTML content here -->
  9. </div>
  10. </div>
  11. </body>

Popover is highly customizable element and you can put anything inside, event another view with navigation.

Popover App Methods

Let’s look at related App methods to work with Popover:

app.popover.create(parameters)- create Popover instance

  • parameters - object. Object with popover parameters

Method returns created Popover’s instance

app.popover.destroy(el)- destroy Popover instance

  • el - HTMLElement or string (with CSS Selector) or object. Popover element or Popover instance to destroy.

app.popover.get(el)- get Popover instance by HTML element

  • el - HTMLElement or string (with CSS Selector). Popover element.

Method returns Popover’s instance

app.popover.open(el, targetEl, animate)- opens Popover

  • el - HTMLElement or string (with CSS Selector). Popover element to open.
  • targetEl - HTMLElement or string (with CSS Selector). Target element to set popover position around this element
  • animate - boolean. Open Popover with animation.

Method returns Popover’s instance

app.popover.close(el, animate)- closes Popover

  • el - HTMLElement or string (with CSS Selector). Popover element to close.
  • animate - boolean. Close Popover with animation.

Method returns Popover’s instance

Popover Parameters

Now let’s look at list of available parameters we need to create Popover:

ParameterTypeDefaultDescription
elHTMLElementPopover element. Can be useful if you already have Popover element in your HTML and want to create new instance using this element
contentstringFull Popover HTML layout string. Can be useful if you want to create Popover element dynamically
backdropbooleantrueEnables Popover backdrop (dark semi transparent layer behind)
backdropElHTMLElement
string
HTML element or string CSS selector of custom backdrop element
closeByBackdropClickbooleantrueWhen enabled, popover will be closed on backdrop click
closeByOutsideClickbooleantrueWhen enabled, popover will be closed on when click outside of it
closeOnEscapebooleanfalseWhen enabled, popover will be closed on ESC keyboard key press
animatebooleantrueWhether the Popover should be opened/closed with animation or not. Can be overwritten in .open() and .close() methods
targetElHTMLElement
string
HTML element or string CSS selector of target element
targetXnumberVirtual target element horizontal offset from left side of the screen. Required without using real target element (targetEl)
targetYnumberVirtual target element vertical offset from top of the screen. Required without using real target element (targetEl)
targetWidthnumber0Virtual target element width (in px). Required without using real target element (targetEl)
targetHeightnumber0Virtual target element height (in px). Required without using real target element (targetEl)
onobject

Object with events handlers. For example:

  1. var popover = app.popover.create({
  2. content: ‘<div class=”popover”>…</div>’,
  3. on: {
  4. opened: function () {
  5. console.log(‘Popover opened’)
  6. }
  7. }
  8. })

Note that all following parameters can be used in global app parameters under popover property to set defaults for all popovers. For example:

  1. var app = new Framework7({
  2. popover: {
  3. closeByBackdropClick: false,
  4. }
  5. });

If you use auto-initialized popovers (e.g. you don’t create them via app.popover.create), it is possible to pass all available popover parameters via data- attributes. For example:

  1. <!-- Pass parameters as kebab-case data attributes -->
  2. <div class="popover" data-close-on-escape="true" data-backdrop="false">
  3. ...
  4. </div>

Popover Methods & Properties

So to create Popover we have to call:

  1. var popover = app.popover.create({ /* parameters */ })

After that we have its initialized instance (like popover variable in example above) with useful methods and properties:

Properties
popover.appLink to global app instance
popover.elPopover HTML element
popover.$elDom7 instance with popover HTML element
popover.backdropElBackdrop HTML element
popover.$backdropElDom7 instance with backdrop HTML element
popover.targetElPopover target HTML element
popover.$targetElDom7 instance with popover target HTML element
popover.paramsPopover parameters
popover.openedBoolean prop indicating whether popover is opened or not
Methods
popover.open(targetEl, animate)Open popover around target element. Where
  • targetEl - HTMLElement or string - target element to set popover position around this element
  • animate - boolean (by default true) - defines whether it should be opened with animation
popover.open(animate)Open popover around target element passed on popover creation. Where
  • animate - boolean (by default true) - defines whether it should be opened with animation
popover.close(animate)Close popover. Where
  • animate - boolean (by default true) - defines whether it should be closed with animation
popover.destroy()Destroy popover
popover.on(event, handler)Add event handler
popover.once(event, handler)Add event handler that will be removed after it was fired
popover.off(event, handler)Remove event handler
popover.off(event)Remove all handlers for specified event
popover.emit(event, …args)Fire event on instance

Control Popover With Links

It is possible to open and close required popover (if you have them in DOM) using special classes and data attributes on links:

  • To open popover we need to add “popover-open“ class to any HTML element (prefered to link)

  • To close popover we need to add “popover-close“ class to any HTML element (prefered to link)

  • If you have more than one popover in DOM, you need to specify appropriate popover via additional data-popover=”.my-popover” attribute on this HTML element

According to above note:

  1. <!-- In data-popover attribute we specify CSS selector of popover we need to open -->
  2. <p><a href="#" data-popover=".my-popover" class="popover-open">Open Popover</a></p>
  3. <!-- And somewhere in DOM -->
  4. <div class="popover my-popover">
  5. <div class="popover-inner">
  6. <!-- Link to close popover -->
  7. <a class="link popover-close">Close Popover</a>
  8. </div>
  9. </div>

Popover Events

Popover will fire the following DOM events on popover element and events on app and popover instance:

DOM Events

EventTargetDescription
popover:openPopover Element<div class=”popover”>Event will be triggered when Popover starts its opening animation
popover:openedPopover Element<div class=”popover”>Event will be triggered after Popover completes its opening animation
popover:closePopover Element<div class=”popover”>Event will be triggered when Popover starts its closing animation
popover:closedPopover Element<div class=”popover”>Event will be triggered after Popover completes its closing animation

App and Popover Instance Events

Popover instance emits events on both self instance and app instance. App instance events has same names prefixed with popover.

EventArgumentsTargetDescription
openpopoverpopoverEvent will be triggered when Popover starts its opening animation. As an argument event handler receives popover instance
popoverOpenpopoverapp
openedpopoverpopoverEvent will be triggered after Popover completes its opening animation. As an argument event handler receives popover instance
popoverOpenedpopoverapp
closepopoverpopoverEvent will be triggered when Popover starts its closing animation. As an argument event handler receives popover instance
popoverClosepopoverapp
closedpopoverpopoverEvent will be triggered after Popover completes its closing animation. As an argument event handler receives popover instance
popoverClosedpopoverapp
beforeDestroypopoverpopoverEvent will be triggered right before Popover instance will be destroyed. As an argument event handler receives popover instance
popoverBeforeDestroypopoverapp

CSS Variables

Below is the list of related CSS variables (CSS custom properties).

  1. :root {
  2. --f7-popover-width: 260px;
  3. }
  4. .ios {
  5. --f7-popover-border-radius: 13px;
  6. --f7-popover-box-shadow: none;
  7. --f7-popover-actions-icon-size: 28px;
  8. --f7-popover-bg-color: rgba(255, 255, 255, 0.95);
  9. --f7-popover-actions-label-text-color: rgba(0, 0, 0, 0.45);
  10. }
  11. .ios .theme-dark,
  12. .ios.theme-dark {
  13. --f7-popover-bg-color: rgba(30, 30, 30, 0.95);
  14. --f7-popover-actions-label-text-color: rgba(255, 255, 255, 0.55);
  15. }
  16. .md {
  17. --f7-popover-border-radius: 4px;
  18. --f7-popover-box-shadow: var(--f7-elevation-8);
  19. --f7-popover-actions-icon-size: 24px;
  20. --f7-popover-bg-color: #fff;
  21. --f7-popover-actions-label-text-color: rgba(0, 0, 0, 0.54);
  22. }
  23. .md .theme-dark,
  24. .md.theme-dark {
  25. --f7-popover-bg-color: #1c1c1d;
  26. --f7-popover-actions-label-text-color: rgba(255, 255, 255, 0.54);
  27. }
  28. .aurora {
  29. --f7-popover-width: 200px;
  30. --f7-popover-border-radius: 4px;
  31. --f7-popover-box-shadow: 0 0 3px 0 rgba(0, 0, 0, 0.1), 0 5px 11px 0 rgba(0,0,0,0.28);
  32. --f7-popover-actions-icon-size: 24px;
  33. --f7-popover-bg-color: #fff;
  34. --f7-popover-actions-label-text-color: rgba(0, 0, 0, 0.6);
  35. }
  36. .aurora .theme-dark,
  37. .aurora.theme-dark {
  38. --f7-popover-bg-color: #1c1c1d;
  39. --f7-popover-actions-label-text-color: rgba(255, 255, 255, 0.6);
  40. }

Examples

  1. <body>
  2. ...
  3. <div class="page">
  4. <div class="navbar">
  5. <div class="navbar-bg"></div>
  6. <div class="navbar-inner">
  7. <div class="left">
  8. <!-- Additional "popover-open" class to open popover on link click -->
  9. <!-- In data-popover attribute we specify CSS selector of popover we need to open -->
  10. <a class="link popover-open" href="#" data-popover=".popover-about">About</a>
  11. </div>
  12. <div class="title">Popover</div>
  13. <div class="right">
  14. <a class="link popover-open" href="#" data-popover=".popover-links">Links</a>
  15. </div>
  16. </div>
  17. </div>
  18. <div class="page-content">
  19. <div class="block">
  20. <p>
  21. <!-- In data-popover attribute we specify CSS selector of popover we need to open -->
  22. <a class="link popover-open" href="#" data-popover=".popover-about">Open About Popover</a>
  23. </p>
  24. <p>
  25. <!-- In data-popover attribute we specify CSS selector of popover we need to open -->
  26. <a class="link popover-open" href="#" data-popover=".popover-links">Open Links Popover</a>
  27. </p>
  28. <p>
  29. <a class="link dynamic-popover" href="#">Create Dynamic Popover</a>
  30. </p>
  31. </div>
  32. </div>
  33. </div>
  34. ...
  35. <!-- Popovers -->
  36. <div class="popover popover-about">
  37. <div class="popover-inner">
  38. <div class="block">
  39. <p>About</p>
  40. <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque ac diam ac quam euismod porta vel a nunc. Quisque sodales scelerisque est, at porta justo cursus ac.</p>
  41. </div>
  42. </div>
  43. </div>
  44. <div class="popover popover-links">
  45. <div class="popover-inner">
  46. <div class="list">
  47. <ul>
  48. <li><a class="list-button item-link" href="#">Link 1</a></li>
  49. <li><a class="list-button item-link" href="#">Link 2</a></li>
  50. <li><a class="list-button item-link" href="#">Link 3</a></li>
  51. <li><a class="list-button item-link" href="#">Link 4</a></li>
  52. </ul>
  53. </div>
  54. </div>
  55. </div>
  56. ...
  57. </body>
  1. var app = new Framework7();
  2. var $$ = Dom7;
  3. // DOM events for About popover
  4. $$('.popover-about').on('popover:open', function (e) {
  5. console.log('About popover open');
  6. });
  7. $$('.popover-about').on('popover:opened', function (e) {
  8. console.log('About popover opened');
  9. });
  10. // Create dynamic Popover
  11. var dynamicPopover = app.popover.create({
  12. targetEl: 'a.dynamic-popover',
  13. content: '<div class="popover">'+
  14. '<div class="popover-inner">'+
  15. '<div class="block">'+
  16. '<p>Popover created dynamically.</p>'+
  17. '<p><a href="#" class="link popover-close">Close me</a></p>'+
  18. '</div>'+
  19. '</div>'+
  20. '</div>',
  21. // Events
  22. on: {
  23. open: function (popover) {
  24. console.log('Popover open');
  25. },
  26. opened: function (popover) {
  27. console.log('Popover opened');
  28. },
  29. }
  30. });
  31. // Events also can be assigned on instance later
  32. dynamicPopover.on('close', function (popover) {
  33. console.log('Popover close');
  34. });
  35. dynamicPopover.on('closed', function (popover) {
  36. console.log('Popover closed');
  37. });
  38. // Open dynamic popover
  39. $$('.dynamic-popover').on('click', function () {
  40. dynamicPopover.open();
  41. });

← Picker

Popup →