GithubHelp home page GithubHelp logo

shubhamuniyal / ngx-tribute Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ladderio/ngx-tribute

0.0 1.0 0.0 1.42 MB

Angular 2+ directive for Zurb Tribute

License: MIT License

JavaScript 17.13% TypeScript 80.29% CSS 0.86% HTML 1.72%

ngx-tribute's Introduction

ngx-tribute

This is an Angular 2+ wrapper directive for native JS library for @mentions - tributejs. It supports contenteditable and Reactive Forms.

To install:

npm i tributejs ngx-tribute

Note: At least version 3.3.5 of tributejs is required.

Usage

Import NgxTributeModule:

import {NgxTributeModule} from 'ngx-tribute';

@NgModule({
  imports: [
    NgxTributeModule
  ],
  ...
})
export class AppModule { }

and use [ngxTribute] directive on your input, textarea or [contenteditable]:

<input [ngxTribute]="tributeOptions">

Import default CSS from node_modules/tributejs/dist/tribute.css to get a minimal working example.

Check tributejs docs for a detailed description of the configuration object.

Check a demo and it's code to see different ways of using the directive. It integrates well with template forms and reactive forms.

API Docs

Inputs:

  • [ngxTribute] - Tribute.js configuration object, refer to original library docs for details
  • [implicitFormControl] - pass FormControl if it's value will be altered by Tribute (this isn't required if you use [formControl] or [formControlName] directives)
  • [menuContainer] - if you want to use menuContainer option from Tribute.js, pass element obtained by template ref variable here, instead of manually retrieving element from DOM. See "Using menuContainer" below for an explanation and example

Outputs:

  • (onMentioned) - emits the whole value of input each time when a new mention is added
  • (mentionItemSelected) - emits the original object of input each time when a new mention is added

Reactive forms

When using Angular reactive forms, form control value needs to be updated whenever you select any mention. This directive does it automatically by intercepting Angular [formControl] or [formControlName] directive attached to the same element as Tribute.js or one of its ancestors. If for some reason you're not using standard directives, you can use [implicitFormControl] input to pass form control, which should be updated, directly to ngxTribute directive.

See demo app and it's code for an example.

Using menuContainer

Tribute.js allows to pass a DOM element, to which menu should be attached, by using menuContainer option. It's a bad practice in Angular to obtain elements via document methods (eg. document.getElementById()). Angular gives it's own methods for accessing DOM nodes. In this case, Template Reference Variable should be used.

See following example:

@Component({
    template: `
        <input [ngxTribute]="options" [menuContainer]="container">
        <div #container></div>
    `
})
class MyComponent {
    options = {
        values: [
            { key: 'foo', value: 'Foo' },
            { key: 'bar', value: 'Bar' },
            { key: 'baz', value: 'Baz' }
        ],
        positionMenu: false
    }
}

Contributing

  • library code is located in projects/ngx-tribute and demo code in src
  • run npm install after cloning the repo
  • run npm run build-lib to build the library used by demo app
  • run npm start to start a dev server with demo app
  • whenever you change something in the library code, you need to run npm run build-lib to see that changes in demo app
  • after finishing, run npm run build-demo to rebuild a demo app

Story behind this lib

We were rewriting a project from AngularJS 1.5 to Angular 2 and faced the lack of a good library for @mentions in the new Angular. There were some, but we needed a good support for [contenteditable], and none of existing solutions satisfied our needs. We decided to check for some lib written in pure JS and add an Angular wrapper for it. We were so happy with the result, that we decided to share that with the rest of the world :)

This is just a tribute

ngx-tribute's People

Contributors

agarbund avatar alex61nn5 avatar

Watchers

James Cloos 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.