Panel / Side Panels - 图1

Panel / Side Panels

Panel Layout

Let’s look how to add Side Panels to our App. We may include up to 2 panels to our App, one on left side and another one on right side. We should add panels’ HTML right in the beginning of the app root element (or <body> in case there is no root element in use):

  1. <body>
  2. <!-- App root element -->
  3. <div id="app">
  4. <!-- Left panel -->
  5. <div class="panel panel-left">
  6. ... panel content goes here ...
  7. </div>
  8. <!-- Right panel -->
  9. <div class="panel panel-right">
  10. ... panel content goes here ...
  11. </div>
  12. ...
  13. </div>
  14. </body>

After we added panels we need to choose opening effect for each panel. There could be one of two effects: “Reveal” (when panel moves out whole app’s content) or “Cover” (when panel overlays app’s content). If you want to use “Reveal” effect you should add additional panel-reveal class to Panel, or panel-cover for cover effect:

  1. <body>
  2. <!-- App root element -->
  3. <div id="app">
  4. <!-- Left panel, let it be with reveal effect -->
  5. <div class="panel panel-left panel-reveal">
  6. ... panel content goes here ...
  7. </div>
  8. <!-- Right panel, with cover effect -->
  9. <div class="panel panel-right panel-cover">
  10. ... panel content goes here ...
  11. </div>
  12. ...
  13. </div>
  14. </body>

Each Panel can be resizable. To make Panel resizable we just need to add panel-resizable class to Panel element:

  1. <body>
  2. <!-- App root element -->
  3. <div id="app">
  4. <!-- Make left panel resizable -->
  5. <div class="panel panel-left panel-resizable">
  6. ... panel content goes here ...
  7. </div>
  8. <!-- Make right panel resizable -->
  9. <div class="panel panel-right panel-resizable">
  10. ... panel content goes here ...
  11. </div>
  12. ...
  13. </div>
  14. </body>

Panel App Methods

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

app.panel.create(parameters)- create Panel instance

  • parameters - object. Object with panel parameters

Method returns created Panel’s instance

app.panel.destroy(el)- destroy Panel instance

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

app.panel.get(el)- get Panel instance by HTML element

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

Method returns Panel’s instance

app.panel.open(panel, animate)- open panel

  • panel - HTMLElement or string (with CSS Selector) of panel element to open
  • animate - boolean. Should it be opened with animation or not. Optional, by default is true

app.panel.close(panel, animate)- close panel

  • panel - HTMLElement or string (with CSS Selector) of panel element to close
  • animate - boolean. Should it be closed with animation or not. Optional, by default is true

app.panel.toggle(panel, animate)- toggle panel

  • panel - HTMLElement or string (with CSS Selector) of panel element to toggle
  • animate - boolean. Should it be opened/closed with animation or not. Optional, by default is true

Panel Parameters

If we create Panel manually using app.panel.create method we need to pass object with panel parameters:

ParameterTypeDefaultDescription
elHTMLElementPanel element
resizablebooleanEnables resizable panel. If not passed then will be determined based on panel-resizable class.
visibleBreakpointnumberMinimal app width (in px) when left panel becomes always visible
collapsedBreakpointnumberMinimal app width (in px) when left panel becomes partially visible (collapsed)
swipebooleanfalseEnable if you want to enable ability to open/close panel with swipe
swipeNoFollowbooleanfalseFallback option for potentially better performance on old/slow devices. If you enable it, then swipe panel will not follow your finger during touch move, it will be automatically opened/closed on swipe left/right.
swipeActiveAreanumber0Width (in px) of invisible edge from the screen that triggers panel swipe
swipeOnlyClosebooleanfalseThis parameter allows to close (but not open) panel with swipes. (swipe should be also enabled)
swipeThresholdnumber0Panel will not move with swipe if “touch distance” will be less than this value (in px).
backdropbooleantrueEnables Panel backdrop (dark semi transparent layer behind)
backdropElHTMLElement
string
HTML element or string CSS selector of custom backdrop element
closeByBackdropClickbooleantrueEnable/disable ability to close panel by clicking outside of panel
onobject

Object with events handlers. For example:

  1. var panel = app.panel.create({
  2. el: ‘.panel-left’,
  3. on: {
  4. opened: function () {
  5. console.log(‘Panel opened’)
  6. }
  7. }
  8. })

For example:

  1. var panel = app.panel.create({
  2. el: '.panel-left',
  3. resizable: true,
  4. visibleBreakpoint: 1024,
  5. collapsedBreakpoint: 768,
  6. })

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

  1. var app = new Framework7({
  2. panel: {
  3. swipe: true,
  4. visibleBreakpoint: 1024,
  5. }
  6. });

Panel Methods & Properties

After we created Panel instance (by calling app.panel.create) or after we got Panel instance (by calling app.panel.get) we may use its useful methods and properties:

Properties
panel.appLink to global app instance
panel.sideString with panel side: left or right
panel.effectString with panel effect: cover or reveal
panel.openedBoolean property indicating whether it is opened or not
panel.elPanel HTML element
panel.$elDom7 instance with panel HTML element
panel.backdropElBackdrop HTML element
panel.$backdropElDom7 instance with backdrop HTML element
panel.paramsPanel parameters
Methods
panel.open(animate)Open panel. Where
  • animate - boolean (by default true) - defines whether it should be opened with animation or not
panel.close(animate)Close panel. Where
  • animate - boolean (by default true) - defines whether it should be closed with animation or not
panel.toggle(animate)Toggle panel. Where
  • animate - boolean (by default true) - defines whether it should be closed with animation or not
panel.enableVisibleBreakpoint()Enable visible breakpoint
panel.disableVisibleBreakpoint()Disable visible breakpoint
panel.toggleVisibleBreakpoint()Toggle visible breakpoint
panel.enableCollapsedBreakpoint()Enable collapsed breakpoint
panel.disableCollapsedBreakpoint()Disable collapsed breakpoint
panel.toggleCollapsedBreakpoint()Toggle collapsed breakpoint
panel.enableResizable()Enable resizable panel
panel.disableResizable()Disable resizable panel
panel.enableSwipe()Enable swipeable panel
panel.disableSwipe()Disable swipeable panel
panel.destroy()Destroy panel instance
panel.on(event, handler)Add event handler
panel.once(event, handler)Add event handler that will be removed after it was fired
panel.off(event, handler)Remove event handler
panel.off(event)Remove all handlers for specified event
panel.emit(event, …args)Fire event on instance

Panel Events

Panel will fire the following DOM events on panel element and events on app and panel instance:

DOM Events

EventTargetDescription
panel:openPanel Element<div class=”panel”>Event will be triggered when Panel starts its opening animation
panel:openedPanel Element<div class=”panel”>Event will be triggered after Panel completes its opening animation
panel:closePanel Element<div class=”panel”>Event will be triggered when Panel starts its closing animation
panel:closedPanel Element<div class=”panel”>Event will be triggered after Panel completes its closing animation
panel:backdrop-clickPanel Overlay Element<div class=”panel-backdrop”>Event will be triggered when the panel overlay is clicked
panel:swipeopenPanel Element<div class=”panel”>Event will be triggered in the very beginning of opening it with swipe
panel:swipePanel Element<div class=”panel”>Event will be triggered for swipe panel during touch swipe action
panel:collapsedbreakpointPanel Element<div class=”panel”>Event will be triggered when it becomes visible/hidden when app width matches its collapsedBreakpoint
panel:breakpointPanel Element<div class=”panel”>Event will be triggered when it becomes visible/hidden when app width matches its visibleBreakpoint
panel:resizePanel Element<div class=”panel”>Event will be triggered on resizable panel resize
panel:beforedestroyPanel Element<div class=”panel”>Event will be triggered right before Panel instance will be destroyed

App and Panel Instance Events

Panel instance emits events on both self instance and app instance. App instance events has same names prefixed with panel.

EventTargetArgumentsDescription
openpanel(panel)Event will be triggered when Panel starts its opening animation. As an argument event handler receives panel instance
panelOpenapp(panel)
openedpanel(panel)Event will be triggered when Panel completes its opening animation. As an argument event handler receives panel instance
panelOpenedapp(panel)
closepanel(panel)Event will be triggered when Panel starts its closing animation. As an argument event handler receives panel instance
panelCloseapp(panel)
closedpanel(panel)Event will be triggered when Panel completes its closing animation. As an argument event handler receives panel instance
panelClosedapp(panel)
backdropClickpanel(panel)Event will be triggered when the panel backdrop is clicked. As an argument event handler receives panel instance
panelBackdropClickapp(panel)
swipeOpenpanel(panel)Event will be triggered in the very beginning of opening it with swipe. As an argument event handler receives panel instance
panelSwipeOpenapp(panel)
swipepanel(panel, progress)Event will be triggered for swipe panel during touch swipe action. As an argument event handler receives panel instance and opened progress (from 0 to 1)
panelSwipeapp(panel, progress)
collapsedBreakpointpanel(panel)Event will be triggered when it becomes visible/hidden when app width matches its collapsedBreakpoint. As an argument event handler receives panel instance
panelCollapsedBreakpointapp(panel)
breakpointpanel(panel)Event will be triggered when it becomes visible/hidden when app width matches its visibleBreakpoint. As an argument event handler receives panel instance
panelBreakpointapp(panel)
resizepanel(panel, newPanelWidth)Event will be triggered on resizable panel resize
panelResizeapp(panel)
beforeDestroypanel(panel)Event will be triggered right before Panel instance will be destroyed
panelBeforeDestroyapp(panel)

Panel Auto Initialization

If you don’t need to use Panel API and your Panel is inside of the app on init or inside of the page and presented in DOM on moment of page initialization then it can be auto initialized with just adding additional panel-init class:

  1. <!-- Add panel-init class -->
  2. <div class="panel panel-left panel-cover panel-init">
  3. ...
  4. </div>

In this case if you need to access created Panel instance you can use the app.panel.get app method:

  1. var panel = app.panel.get('.panel-left');
  2. if (panel.opened) {
  3. // do something
  4. }

When using auto init you may need to pass additional parameters. It can be done with data- attributes on panel element.

  1. <!-- parameters set via data- attributes -->
  2. <div
  3. class="panel palen-left panel-reveal panel-init"
  4. data-collapsed-breakpoint="768"
  5. data-visible-breakpoint="1024"
  6. data-swipe="true"
  7. >
  8. ...
  9. </div>

Parameters used in camelCase, for example visibleBreakpoint, in data- attributes should be used in kebab-case as data-visible-breakpoint

Control Panel With Links

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

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

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

  • To toggle panel we need to add panel-toggle class to any HTML element (prefered to link)

  • If you want to specify which panel should opened/closed, then it could be done via additional data-panel=".panel-left" attribute on this HTML element. This attribute can also receive just left or right value if there is only one panel of such side in DOM.

According to above note:

  1. <body>
  2. <div id="app">
  3. <!-- Left Panel with Reveal effect -->
  4. <div class="panel panel-left panel-reveal panel-init">
  5. <div class="block">
  6. ...
  7. <!-- Clicking on link with "panel-close" class will close panel -->
  8. <p><a href="#" class="panel-close">Close me</a></p>
  9. <!-- Click on link with "panel-open" and data-panel=".panel-right" attribute will open Right panel -->
  10. <p><a href="#" data-panel=".panel-right" class="panel-open">Open Right Panel</a></p>
  11. </div>
  12. </div>
  13. <!-- Right Panel with Cover effect -->
  14. <div class="panel panel-right panel-cover panel-init">
  15. <div class="block">
  16. ...
  17. <!-- Click on link with "panel-close" class will close panel -->
  18. <p><a href="#" class="panel-close">Close me</a></p>
  19. <!-- Click on link with "panel-open" and data-panel=".panel-left" attribute will open Left panel -->
  20. <p><a href="#" data-panel=".panel-left" class="panel-open">Open Left Panel</a></p>
  21. </div>
  22. </div>
  23. ...
  24. <div class="page-content">
  25. <div class="block">
  26. <!-- Click on link with "panel-open" and data-panel=".panel-left" attribute will open Left panel -->
  27. <p><a href="#" data-panel=".panel-left" class="panel-open">Open Left Panel</a></p>
  28. <!-- Click on link with "panel-open" and data-panel=".panel-right" attribute will open Right panel -->
  29. <p><a href="#" data-panel=".panel-right" class="panel-open">Open Right Panel</a></p>
  30. </div>
  31. </div>
  32. </div>
  33. ...
  34. </body>

Routable Panels

Panels can also be routable with same features as for routable modals and pages:

  • it provides opportunity to open Panel by usual links instead of so called special links or API,
  • with enabled Push State, the same Panel will be opened when you refresh browser, navigate back and forward in history,
  • with routable Panels you can load Panel itself and its content in the same ways as for pages and modals, i.e. using url, content, template, templateUrl, component or componentUrl
  1. routes = [
  2. ...
  3. // Creates Panel from passed HTML string
  4. {
  5. path: '/left-panel/',
  6. panel: {
  7. content: `
  8. <div class="panel panel-left panel-cover">
  9. <div class="view">
  10. <div class="page">
  11. ...
  12. </div>
  13. </div>
  14. </div>
  15. `
  16. }
  17. },
  18. // Load Panel from file via Ajax
  19. {
  20. path: '/right-panel-ajax/',
  21. panel: {
  22. url: './right-panel.html',
  23. /* right-panel.html contains:
  24. <div class="panel panel-right panel-reveal">
  25. <div class="view">
  26. <div class="page">
  27. ...
  28. </div>
  29. </div>
  30. </div>
  31. */
  32. },
  33. },
  34. // Load Panel from component file
  35. {
  36. path: '/panel-component/',
  37. panel: {
  38. componentUrl: './panel-component.html',
  39. /* panel-component.html contains:
  40. <template>
  41. <div class="panel panel-left panel-cover">
  42. <div class="view">
  43. <div class="page">
  44. ...
  45. </div>
  46. </div>
  47. </div>
  48. </template>
  49. <style>...</style>
  50. <script>...</script>
  51. */
  52. },
  53. },
  54. ]

According to example above:

  • when you click on link with /left-panel/ href attribute it will open Panel from specified string content,
  • when you click on link with /right-panel-ajax/ href attribute it will perform Ajax request to right-panel.html file and open it as a Right Panel,
  • when you click on link with /panel-component/ href attribute it will perform Ajax request to panel-component.html file, parse it as a Router Component and open it as a Panel,

CSS Variables

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

Note that commented variables are not specified by default and their values is what they fallback to in this case.

  1. :root {
  2. --f7-panel-width: 260px;
  3. /*
  4. --f7-panel-left-width: var(--f7-panel-width);
  5. --f7-panel-right-width: var(--f7-panel-width);
  6. --f7-panel-left-collapsed-width: var(--f7-panel-collapsed-width);
  7. --f7-panel-right-collapsed-width: var(--f7-panel-collapsed-width);
  8. */
  9. --f7-panel-bg-color: #fff;
  10. }
  11. .ios {
  12. --f7-panel-collapsed-width: 58px;
  13. --f7-panel-backdrop-bg-color: rgba(0, 0, 0, 0);
  14. --f7-panel-transition-duration: 400ms;
  15. --f7-panel-shadow: transparent;
  16. }
  17. .md {
  18. --f7-panel-collapsed-width: 60px;
  19. --f7-panel-backdrop-bg-color: rgba(0, 0, 0, 0.3);
  20. --f7-panel-transition-duration: 300ms;
  21. --f7-panel-shadow: rgba(0, 0, 0, 0.2) 0%,
  22. rgba(0, 0, 0, 0.07) 30%,
  23. rgba(0, 0, 0, 0.03) 40%,
  24. rgba(0, 0, 0, 0) 60%,
  25. rgba(0, 0, 0, 0) 100%;
  26. }
  27. .aurora {
  28. --f7-panel-collapsed-width: 44px;
  29. --f7-panel-backdrop-bg-color: rgba(0, 0, 0, 0.2);
  30. --f7-panel-transition-duration: 400ms;
  31. --f7-panel-shadow: transparent;
  32. }

Examples

  1. <body>
  2. <div id="app">
  3. <div class="panel panel-left panel-reveal panel-resizable panel-init">
  4. <div class="block">
  5. <p>Left Panel content here</p>
  6. <p><a class="panel-close" href="#">Close me</a></p>
  7. <p><a class="panel-open" href="#" data-panel=".panel-right-1">Open Right Panel</a></p>
  8. </div>
  9. </div>
  10. <div class="panel panel-right panel-right-1 panel-cover panel-resizable panel-init">
  11. <div class="block">
  12. <p>Right Panel content here</p>
  13. <p><a class="panel-close" href="#">Close me</a></p>
  14. <p><a class="panel-open" href="#" data-panel=".panel-left">Open Left Panel</a></p>
  15. </div>
  16. </div>
  17. <div class="panel panel-right panel-right-2 panel-cover panel-init theme-dark">
  18. <div class="page">
  19. <div class="block">
  20. <p>Here comes another 3rd panel.</p>
  21. <p><a class="panel-close" href="#">Close me</a></p>
  22. </div>
  23. </div>
  24. </div>
  25. <div class="view view-main view-init">
  26. <div class="page">
  27. <div class="navbar">
  28. <div class="navbar-bg"></div>
  29. <div class="navbar-inner">
  30. <div class="left"></div>
  31. <div class="title">Panels</div>
  32. <div class="right"></div>
  33. </div>
  34. </div>
  35. <div class="page-content">
  36. <div class="block block-strong">
  37. <p><a class="button panel-open" href="#" data-panel=".panel-left">Open Left Panel</a></p>
  38. <p><a class="button panel-open" href="#" data-panel=".panel-right-1">Open Right Panel</a></p>
  39. <p><a class="button panel-open" href="#" data-panel=".panel-right-2">Open Another Panel</a></p>
  40. </div>
  41. </div>
  42. </div>
  43. </div>
  44. </div>
  45. </body>
  1. /* Limit resizable panel width */
  2. .panel {
  3. min-width: 100px;
  4. max-width: 90vw;
  5. }
  1. var app = new Framework7();
  2. var $$ = Dom7;
  3. // Dom Events
  4. $$('.panel-left').on('panel:open', function () {
  5. console.log('Panel left: open');
  6. });
  7. $$('.panel-left').on('panel:opened', function () {
  8. console.log('Panel left: opened');
  9. });
  10. // Instance Events
  11. var panelRight = app.panel.get('.panel-right-1');
  12. panelRight.on('open', function () {
  13. console.log('Panel right: open');
  14. });
  15. panelRight.on('opened', function () {
  16. console.log('Panel right: opened');
  17. });
  18. // App Events
  19. app.on('panelClose', function (panel) {
  20. console.log('Panel ' + panel.side + ': close');
  21. });
  22. app.on('panelClosed', function (panel) {
  23. console.log('Panel ' + panel.side + ': closed');
  24. });
  25. app.on('panelResize', function (panel, newPanelWidth) {
  26. console.log('Panel resized to ' + newPanelWidth + 'px');
  27. });

← Page

Photo Browser →