Giter VIP home page Giter VIP logo

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

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

ember-context-menu's Issues

Getting Context Menu to work inside Ember Light Table

I'm trying to make the context menu work inside an Ember Light Table.
So far, upon right click, I make sure that the row you clicked on is the selected row and bring up a context menu.
But inside actions (of contextItems) I seem unable to do anything to determine the selected row and call another method (must be inside the component, cannot outside like you got in the dummy)

Ember n00b, please bear with me....


    contextItems: [
        {
            label: 'Publish Period',
            action(selection, details, event) { 
                  console.log(this);  // = undefined
                  // selectio = []
                  // details = undefined
                  // event = jquery object
            }
        }
    ],

missing dependencies

To get this working I had to:

ember install ember-wormhole
ember install ember-invoke-action

Perhaps these two should be in "dependencies" instead of "devDependencies"?

Update to work without mixins

@cbroeren would you be open to updating this to not use mixins and instead set the contextmenu call up directly? I have it working in my app with {{on "contextmenu" this.contextMenuTrigger}}

outdated FontAwesome implementation

currently the dependency "ember-font-awesome" (v4) breaks the ("@fortawesome/ember-fontawesome" v5) implementation and makes Version 5 Fonts unusable.

I cant find any way to disable the "ember-font-awesome" package to use my project fontawesome implementation:
even not through the assets exclude options: https://github.com/martndemus/ember-font-awesome#excluding-assets

package.json contain the following dependencies:
"@fortawesome/ember-fontawesome": "^0.2.1",
"@fortawesome/free-brands-svg-icons": "^5.9.0",
"@fortawesome/free-regular-svg-icons": "^5.9.0",
"@fortawesome/free-solid-svg-icons": "^5.9.0",

The Logic that create an Icon for the node can also be done manually in the template and will not break any project dependencies.

Is it possible to remove the outdated "ember-font-awesome" implementation or update to a up-to-date package like:
https://github.com/FortAwesome/ember-fontawesome ?

Add font-awesome functionality back

Hey,
ember-font-awesome has been updated recently and no longer has any bower dependencies, font-awesome is installed via npm.

Was the bower dependency the reason font-awesome functionality was removed? If so could it be re-enabled?

Icon Error

When specifying an icon for a menu item I get an error "Error: Compile Error: fa-icon is not a helper"

I did "ember install ember-font-awesome" without any luck

Acceptance Tests

Does anyone know how to test this add-on with an acceptance test?

I tried like this
triggerEvent('.file-item:first', 'contextmenu');
and got this error
Uncaught Error: Assertion Failed: You need to pass event to the context-menu activate()

I would appreciate very much if anyone could help me.

Adding keyboard navigation

It should be possible to navigate context menu using the keyboard.
i.e.

  • using "arrow keys" to go up or down
  • using "enter" to execute the item action
  • using "escape" to close the menu

What's the easy way to add this functionality?
Would be possible to make it available by default?

Bug: Out of screen

When it is too large, has too many sub-actions or was triggered almost at the right of te screen, it's going out of view. Should wrap it inside the window.

menu does not display

Hey there,

I am able to catch the right click event on my component in _contextMenu(e){ } but it appears that my contextItems array is not found. Should the label attribute of each object in contextItems be displayed as a menu item without further configuration? Thanks for this add-on!

Style Elements differently

Hi,

I'm wondering how I can style elements different.
E.g. I want to give my "delete" option "color: red".
I also would like to add "hr" separators.

It would help when a contextItem would support a "class" attribute.

Cheers,
Pirmin

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.