Giter Site home page Giter Site logo

amiarslimane / ember-context-menu Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cbroeren/ember-context-menu

0.0 0.0 0.0 329 KB

An ember addon for custom context-menu

Home Page: https://cbroeren.github.com/ember-context-menu

License: MIT License

JavaScript 86.11% HTML 4.98% Handlebars 3.76% SCSS 5.16%

ember-context-menu's Introduction

ember-context-menu

npm version Build Status Ember Observer Score Code Climate

An ember-cli addon to add any right-click-menu to your components. Try it here

Compatibility

  • Ember.js v3.16 or above
  • Ember CLI v2.13 or above
  • Node.js v10 or above

Installation

In your application's directory:

$ ember install ember-context-menu

In your application.hbs add the following:

{{context-menu}}

WARNING: You need to add this to make the context-menu work, and should add it just once in your application.

Usage

Using the mixin

This mixin is designed to add a context-menu to any component. Add it to your component like this:

import Component from '@ember/component';
import contextMenuMixin from 'ember-context-menu';

export default Component.extend(contextMenuMixin, {
  // your component properties
  
  _contextMenu(e) {
    // do anything before triggering the context-menu
  }
});

Context items

Your component needs at least an array of contextItems, which should have a label and an action.

import Component from '@ember/component';

export default Component.extend(contextMenuMixin, {
  contextItems: [
    {
      label: 'do something',
      action(selection, details, event) { /* do something */ }
    }
  ]
});

Label icons

๐Ÿšซ (Temporary removed from 0.2.0, back in 0.3.2)

You can optionally set an icon to show in front of the label. Just give the name of the icon.

  contextItems: [
    {
      label: 'do something',
      icon: 'search',
      action() { /* do something */ }
    }
  ]

The icons that you can use are the one from font-awesome. See http://fontawesome.io/icons/ for the icons to use. Special thanks to the ember-font-awesome addon.

Sub actions

You can add as many sub-actions as you like, but keep in mind it could blow out of your screen ;-)

  contextItems: [
    {
      label: 'multiple actions',
      subActions: [
        {
          label: 'sub action 1',
          action() { /* do something */ }
        }
      ]
    }
  ]

Selection

This context-menu can even be used in case you have to pass an item to your action. You should add it as the contextSelection. This could be one or multiple items.

  contextItems: [
    {
      label: 'do something',
      action(selection) { /* do something with the selection */ }
    }
  ],
  
  contextSelection: { foo: 'bar' }

When it's an array of multiple items, the context-menu will show the amount of items you pass to the action.

Details

If you want to pass some more details to your action, you can set is as the contextDetails. It will be passed to the action as the second argument.

  contextItems: [
    {
      label: 'do something',
      action(selection, details) { /* do something */ }
    }
  ],
  
  contextDetails: { foo: 'bar' }

Disabled actions

When your item has no action and no sub-actions, it will be disabled by default. Also you could disable it by yourself to add the disabled property. This could be either a boolean or a function which gets the selection.

  contextItems: [
    {
      label: 'foo',
      disabled: true
      action() { /* do nothing */ }
    },
    {
      label: 'bar',
      disabled(selection) {
        /* return disabled depending on selection */
      },
      action() { /* do something */ }
    }
  ]

Custom styling

The addon has some predefined styling to just get a quick start. You are able to change the styling of the complete menu by overwriting the styling for the following classes:

  • context-menu
  • context-menu--sub
  • context-menu__item
  • context-menu__item--disabled
  • context-menu__item--parent
  • context-menu__item__label

(Set up by the BEM convention)

Example screenshot Example screenshot with multiple selection

Testing

Test helpers are provided to make it easier to trigger the context menu to open.

import { triggerContextMenu } from 'ember-context-menu/test-support';

triggerContextMenu('.my-class-selector');

Special thanks to @Fabriquartz (Fabriquartz.com)

Contributing

See the Contributing guide for details.

License

This project is licensed under the MIT License.

ember-context-menu's People

Contributors

bekzod avatar brianhjelle avatar cbroeren avatar dependabot[bot] avatar ember-tomster avatar florisvanvoorst avatar michaelw8 avatar robbiethewagner avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.