Action Sheet

Action Sheet is a slide-up pane for presenting the user with a set of alternatives for how to proceed with a given task.

You can also use action sheets to prompt the user to confirm a potentially dangerous action.

The action sheet contains an optional title and one or more buttons, each of which corresponds to an action to take.

Note that it is not recommended to use Action Sheet on large screens (iPad). On large screens you should use Popover instead.

Action Sheet App Methods

Lets look at related App methods to work with the Action Sheet:

app.actions.create(parameters)- create Action Sheet instance

  • parameters - object. Object with action sheet parameters

Method returns created Action Sheet's instance

app.actions.destroy(el)- destroy Action Sheet instance

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

app.actions.get(el)- get Action Sheet instance by HTML element

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

Method returns Action Sheet's instance

app.actions.open(el, animate)- opens Action Sheet

  • el - HTMLElement or string (with CSS Selector). Action Sheet element to open.
  • animate - boolean. Open Actions Sheet with animation.

Method returns Action Sheet's instance

app.actions.close(el, animate)- closes Action Sheet

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

Method returns Action Sheet's instance

Action Sheet Parameters

Now lets look at a list of available parameters we need to create the Action Sheet:

ParameterTypeDefaultDescription
elHTMLElementAction Sheet element. Can be useful if you already have Action Sheet element in your HTML and want to create new instance using this element
contentstringFull Action Sheet HTML content string. Can be useful if you want to create Action Sheet element with custom HTML
backdropbooleantrueEnables Action Sheet backdrop (dark semi transparent layer behind)
closeByBackdropClickbooleantrueWhen enabled, action sheet will be closed on backdrop click
closeByOutsideClickbooleanfalseWhen enabled, action sheet will be closed on when click outside of it
animatebooleantrueWhether the Action Sheet should be opened/closed with animation or not. Can be overwritten in .open() and .close() methods
buttonsarrayAction sheet groups/buttons. In this case Actions layout will be generated dynamically based on passed groups and buttons. In case of groups it should array where each item represent array with buttons for group.
gridbooleanfalseEnables grid buttons layout
convertToPopoverbooleantrueWhen enabled, action sheet will be converted to Popover on large screens.
forceToPopoverbooleanfalseWhen enabled, action sheel will be always converted to Popover.
targetElHTMLElement
string
HTML element or string CSS selector of target element. Required when converstion to popover is in use
targetXnumberVirtual target element horizontal offset from left side of the screen. Required when converstion to popover is in use without using real target element (targetEl)
targetYnumberVirtual target element vertical offset from top of the screen. Required when converstion to popover is in use without using real target element (targetEl)
targetWidthnumber0Virtual target element width (in px). Required when converstion to popover is in use without using real target element (targetEl)
targetHeightnumber0Virtual target element height (in px). Required when converstion to popover is in use without using real target element (targetEl)
onClickfunction(actions, e)Callback function that will be executed after click on the Action Sheet button
renderfunctionCustom function to render Action Sheet. Must return Action Sheet html
renderPopoverfunctionCustom function to render Popover when conversition to popover is in use. Must return Popover html
onobjectObject with events handlers. For example:
var actions = app.actions.create({
  buttons: [ /* ... */ ],
  on: {
    opened: function () {
      console.log('Action Sheet opened')
    }
  }
})

Note that all of the following parameters can be used in the global app parameters under the actions property to set defaults for all action sheets. For example:

var app = new Framework7({
  actions: {
    convertToPopover: false,
    grid: true,
  }
});

Button Parameters

Each Button in buttons array must be presented as an object with button parameters:

ParameterTypeDefaultDescription
textstringString with Button's text (could be HTML string)
iconstringHTML string of icon
boldbooleanfalseEnables bold button text
colorstringButton color, one of default colors
bgstringButton background color, one of default colors
labelbooleanfalseIf enabled then it will be rendered as label instead of button
disabledbooleanfalseDefines whether the button is disabled or not.
closebooleantrueIf enabled then button click will close Action Sheet
onClickfunction(actions, e)Callback function that will be executed after click on this button

Action Sheet Methods & Properties

So to create an Action Sheet we have to call:

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

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

Properties
actions.appLink to global app instance
actions.elAction sheet HTML element
actions.$elDom7 instance with action sheet HTML element
actions.backdropElBackdrop HTML element
actions.$backdropElDom7 instance with backdrop HTML element
actions.paramsAction sheet instance parameters
actions.openedBoolean prop indicating whether action sheet is opened or not
Methods
actions.open(animate)Open action sheet. Where
  • animate - boolean (by default true) - defines whether it should be opened with animation
actions.close(animate)Close action sheet. Where
  • animate - boolean (by default true) - defines whether it should be closed with animation
actions.destroy()Destroy action sheet
actions.on(event, handler)Add event handler
actions.once(event, handler)Add event handler that will be removed after it was fired
actions.off(event, handler)Remove event handler
actions.off(event)Remove all handlers for specified event
actions.emit(event, ...args)Fire event on instance

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

  • To open action sheet we need to add "actions-open" class to any HTML element (prefered to link)

  • To close action sheet we need to add "actions-close" class to any HTML element (prefered to link)

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

According to above note:

<!-- In data-actions attribute we specify CSS selector of action sheet we need to open -->
<p><a href="#" data-actions=".my-actions" class="actions-open">Open Actions</a></p>

<!-- And somewhere in DOM -->
<div class="actions-modal my-actions">
  ...
</div>

Action Sheet Events

Action sheet will fire the following DOM events on action sheet element and events on app and action sheet instance:

DOM Events

EventTargetDescription
actions:openAction Sheet Element<div class="actions-modal">Event will be triggered when Action Sheet starts its opening animation
actions:openedAction Sheet Element<div class="actions-modal">Event will be triggered after Action Sheet completes its opening animation
actions:closeAction Sheet Element<div class="actions-modal">Event will be triggered when Action Sheet starts its closing animation
actions:closedAction Sheet Element<div class="actions-modal">Event will be triggered after Action Sheet completes its closing animation

App and Action Sheet Instance Events

Action Sheet instance emits events on both self instance and app instance. App instance events has same names prefixed with actions.

EventArgumentsTargetDescription
openactionsactionsEvent will be triggered when Action Sheet starts its opening animation. As an argument event handler receives action sheet instance
actionsOpenactionsapp
openedactionsactionsEvent will be triggered after Action Sheet completes its opening animation. As an argument event handler receives action sheet instance
actionsOpenedactionsapp
closeactionsactionsEvent will be triggered when Action Sheet starts its closing animation. As an argument event handler receives action sheet instance
actionsCloseactionsapp
closedactionsactionsEvent will be triggered after Action Sheet completes its closing animation. As an argument event handler receives action sheet instance
actionsClosedactionsapp
beforeDestroyactionsactionsEvent will be triggered right before Action Sheet instance will be destroyed. As an argument event handler receives action sheet instance
actionsBeforeDestroyactionsapp

CSS Variables

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

.ios {
  --f7-actions-bg-color: rgba(255, 255, 255, 0.95);
  --f7-actions-border-radius: 13px;
  --f7-actions-button-border-color: rgba(0, 0, 0, 0.2);
  --f7-actions-button-text-color: var(--f7-theme-color);
  --f7-actions-button-pressed-bg-color: rgba(230, 230, 230, 0.9);
  --f7-actions-button-padding: 0px;
  --f7-actions-button-text-align: center;
  --f7-actions-button-height: 57px;
  --f7-actions-button-height-landscape: 44px;
  --f7-actions-button-font-size: 20px;
  --f7-actions-button-icon-size: 28px;
  --f7-actions-button-justify-content: center;
  --f7-actions-label-padding: 8px 10px;
  --f7-actions-label-text-color: #8a8a8a;
  --f7-actions-label-font-size: 13px;
  --f7-actions-label-justify-content: center;
  --f7-actions-group-border-color: transparent;
  --f7-actions-group-margin: 8px;
  --f7-actions-grid-button-text-color: #757575;
  --f7-actions-grid-button-icon-size: 48px;
  --f7-actions-grid-button-font-size: 12px;
}
.md {
  --f7-actions-bg-color: #fff;
  --f7-actions-border-radius: 0px;
  --f7-actions-button-border-color: transparent;
  --f7-actions-button-text-color: rgba(0, 0, 0, 0.87);
  --f7-actions-button-pressed-bg-color: #e5e5e5;
  --f7-actions-button-padding: 0 16px;
  --f7-actions-button-text-align: left;
  --f7-actions-button-height: 48px;
  --f7-actions-button-height-landscape: 48px;
  --f7-actions-button-font-size: 16px;
  --f7-actions-button-icon-size: 24px;
  --f7-actions-button-justify-content: space-between;
  --f7-actions-label-padding: 12px 16px;
  --f7-actions-label-text-color: rgba(0, 0, 0, 0.54);
  --f7-actions-label-font-size: 16px;
  --f7-actions-label-justify-content: flex-start;
  --f7-actions-group-border-color: #d2d2d6;
  --f7-actions-group-margin: 0px;
  --f7-actions-grid-button-text-color: #757575;
  --f7-actions-grid-button-icon-size: 48px;
  --f7-actions-grid-button-font-size: 12px;
}

Examples

<body>
...
  <div class="page-content">
  <div class="block">
    <p><a class="ac-1" href="#">One group, three buttons</a></p>
    <p><a class="ac-2" href="#">One group, title, three buttons</a></p>
    <p><a class="ac-3" href="#">Two groups</a></p>
    <p><a class="ac-4" href="#">Three groups</a></p>
    <p><a class="ac-5" href="#">With callbacks on click</a></p>
    <p><a class="ac-6" href="#">Actions grid</a></p>
  </div>
</div>
...
</body>
var app = new Framework7();

var $$ = Dom7;

//- One group, three buttons
var ac1 = app.actions.create({
  buttons: [
    {
      text: 'Button1',
      bold: true
    },
    {
      text: 'Button2'
    },
    {
      text: 'Cancel',
      color: 'red'
    },
  ]
})
//- One group, title, three buttons
var ac2 = app.actions.create({
  buttons: [
    {
      text: 'Do something',
      label: true
    },
    {
      text: 'Button1',
      bold: true
    },
    {
      text: 'Button2',
    },
    {
      text: 'Cancel',
      color: 'red'
    },
  ]
});
//- Two groups
var ac3 = app.actions.create({
  buttons: [
    // First group
    [
      {
        text: 'Do something',
        label: true
      },
      {
        text: 'Button1',
        bold: true
      },
      {
        text: 'Button2',
      }
    ],
    // Second group
    [
      {
        text: 'Cancel',
        color: 'red'
      }
    ]
  ]
});
//- Three groups
var ac4 = app.actions.create({
  buttons: [
    [
      {
        text: 'Share',
        label: true
      },
      {
        text: 'Mail',
      },
      {
        text: 'Messages',
      }
    ],
    [
      {
        text: 'Social share',
        label: true
      },
      {
        text: 'Facebook',
      },
      {
        text: 'Twitter',
      }
    ],
    [
      {
        text: 'Cancel',
        color: 'red'
      }
    ]
  ],
});
//- With callbacks on click
var ac5 = app.actions.create({
  buttons: [
    {
      text: 'Button1',
      onClick: function () {
        app.dialog.alert('Button1 clicked')
      }
    },
    {
      text: 'Button2',
      onClick: function () {
        app.dialog.alert('Button2 clicked')
      }
    },
    {
      text: 'Cancel',
      color: 'red',
      onClick: function () {
        app.dialog.alert('Cancel clicked')
      }
    },
  ]
});
//- Actions Grid
var ac6 = app.actions.create({
  grid: true,
  buttons: [
    [
      {
        text: 'Button 1',
        icon: '<img src="https://cdn.framework7.io/placeholder/people-96x96-1.jpg" width="48"/>'
      },
      {
        text: 'Button 2',
        icon: '<img src="https://cdn.framework7.io/placeholder/people-96x96-2.jpg" width="48">'
      },
      {
        text: 'Button 3',
        icon: '<img src="https://cdn.framework7.io/placeholder/people-96x96-3.jpg" width="48">'
      },
    ],
    [
      {
        text: 'Button 1',
        icon: '<img src="https://cdn.framework7.io/placeholder/fashion-96x96-4.jpg" width="48"/>'
      },
      {
        text: 'Button 2',
        icon: '<img src="https://cdn.framework7.io/placeholder/fashion-96x96-5.jpg" width="48">'
      },
      {
        text: 'Button 3',
        icon: '<img src="https://cdn.framework7.io/placeholder/fashion-96x96-6.jpg" width="48">'
      },
    ]
  ]
});

$$('.ac-1').on('click', function () {
    ac1.open();
});
$$('.ac-2').on('click', function () {
    ac2.open();
});
$$('.ac-3').on('click', function () {
    ac3.open();
});
$$('.ac-4').on('click', function () {
    ac4.open();
});
$$('.ac-5').on('click', function () {
    ac5.open();
});
$$('.ac-6').on('click', function () {
    ac6.open();
});