Giter Club home page Giter Club logo

ember-cli-active-link-wrapper's Introduction

ember-cli-active-link-wrapper Ember Observer Score Build Status

A simple link wrapper to wrap active links in an element that inherits the link's active class. Useful for e.g. bootstrap where the active class should be on the containing li not on the a.

Versions

If you are using ember version earlier than 2.3.0, install version 0.2.0 of this addon.

Usage

{{#active-link}}
  {{link-to "Index" "index"}}
{{/active-link}}

Produces (roughly) the markup:

<li class="active">
    <a href="/" class="active">Index</a>
</li>

Installation

ember install ember-cli-active-link-wrapper

Options

There are several options available to adjust functionality:

Option Default Description
tagName 'li' Components HTML tag name
linkSelector 'a.ember-view' jQuery selector for child {{link-to}}'s
activeClass Computed** Class name to apply when any child {{link-to}} is also active
disabledClass Computed** Class name to apply when ALL child {{link-to}}'s are disabled

** Default class names are pulled from the child {{link-to}}, which in turn defaults to 'active'. You can change it on either the child {{link-to}} or directly on the {{active-link}}. See the examples below.

Transition classes

The .ember-transitioning-in and .ember-transitioning-out classes are also mirrored on the containing wrapper if they are present on the child link-to, allowing you to style the wrapper during router transitions.

Examples

Change the element type by defining the tagName.

{{#active-link tagName="div"}}
  {{link-to "Index" "index"}}
{{/active-link}}
<div class="active">
    <a href="/" class="active">Index</a>
</div>

Changing the activeClass on the {{link-to}} will also change it on the {{active-link}}. Or, you can specifically define what the activeClass will be for the {{active-link}}. Similarly, the disabledClass functions the same way.

{{#active-link}}
  {{link-to "Index" "index" activeClass="enabled"}}
{{/active-link}}
{{#active-link activeClass="enabled"}}
  {{link-to "Index" "index"}}
{{/active-link}}
<li class="enabled">
    <a href="/" class="enabled">Index</a>
</li>
<li class="enabled">
    <a href="/" class="active">Index</a>
</li>

The active and/or disabled classes can be disabled (pun intended) by passing boolean false. This causes the class NOT to be applied, even if child {{link-to}}'s are active/disabled.

{{#active-link disabledClass=false}}
  {{link-to "Other" "other" disabled=true}}
{{/active-link}}
<li>
    <a href="/" class="disabled">Index</a>
</li>

If the child {{link-to}}'s have their tagName changed, be sure to adjust the selector. Always include the .ember-view class since all link-to's apply that class.

{{#active-link linkSelector="button.ember-view"}}
  {{link-to "Index" "index" tagName="button"}}
{{/active-link}}
<li class="active">
    <button class="active">Index</button>
</li>

This wrapper is also very useful as a container of a dropdown. Here is an example of a bootstrap dropdown within a navbar.

{{#active-link class="dropdown"}}
  <a class="dropdown-toggle" data-toggle="dropdown">Dropdown <span class="caret"></span></a>
  <ul class="dropdown-menu">
    {{#active-link}}
      {{link-to "Index" "index"}}
    {{/active-link}}
    {{#active-link}}
      {{link-to "Other" "other"}}
    {{/active-link}}
  </ul>
{{/active-link}}
<li class="dropdown active">
  <a class="dropdown-toggle" data-toggle="dropdown">Dropdown <span class="caret"></span></a>
  <ul class="dropdown-menu">
    <li class="active">
      <a href="/" class="active">Index</a>
    </li>
    <li>
      <a href="/other">Other</a>
    </li>
  </ul>
</li>

Mixin

Functionality of the {{active-link}} component has been extracted into a mixin. That way you can import the mixin and use it in other components, such as dropdown's.

// app/components/my-dropdown.js
import Ember from 'ember';
import ActiveLinkMixin from 'ember-cli-active-link-wrapper/mixins/active-link';

export default Ember.Component.extend(ActiveLinkMixin, {
  // your code (or extend from an another addon component)
});

Development

  • git clone this repository
  • npm install
  • bower install

Running

Running Tests

  • npm test (Runs ember try:testall to test against multiple Ember versions)
  • ember test
  • ember test --server

For more information on using ember-cli, visit http://www.ember-cli.com/.

ember-cli-active-link-wrapper's People

Contributors

alexspeller avatar bdami-gavant avatar ember-tomster avatar kernel-io avatar nikz avatar odoe avatar panman82 avatar robdel12 avatar turbo87 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  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  avatar  avatar  avatar  avatar  avatar  avatar

ember-cli-active-link-wrapper's Issues

Set link as active by default

Once again you @alexspeller make addons that save lives ๐Ÿ˜†. Thank you.

Is there a way to set a link as active by default? And when I click on another link it removes the active from that default? I tried to do this here, set my first link as active, but when I click on another link (within that same group) it keeps the active on the two links.

Upgrade ember-cli-babel to v6.6 or greater

I get the following deprecation warning:

DEPRECATION: ember-cli-babel 5.x has been deprecated. Please upgrade to at least ember-cli-babel 6.6. Version 5.2.8 located: myApp -> ember-cli-active-link-wrapper -> ember-cli-babel

Changing active class

Hi, thanks for this small helper. It's great. Would it be possible for it to take the default activeClass?

We normally change active to is-active in our apps.

Ember.LinkView.reopen({
    activeClass: 'is-active'
});

1.13+ Support

In 1.13.0-beta.1 and above, the childViews array is not maintained (HTMLBars maintains a tree structure instead of a flat array listing).

I spiked getting things working for both pre-1.13 and post in this JSBin, snippet below:

  didRender: function() { 
    function filterAttrMorphs(node) {
      return node.constructor.name !== 'AttrMorph';
    }
    var childViews = this._renderNode.childNodes.filter(filterAttrMorphs)[0].childNodes
    .map(function(node) {
      return node.emberView;
    });

    this.set('childViews', Ember.A(childViews));
  }

I am not super happy with the requirement to use the private _renderNodes, and will be trying to get something working in core for later betas, but figured folks might want to use this addon against 1.13+ in the interim.

I'll try to do an actual PR later and add testing for cross Ember versions.

DEPRECATION: An addon is trying to access project.nodeModulesPath.

I get this deprecation in node v8.9.4. Not sure if it is this addon. Perhaps update ember-cli-dependency-checker to : "^2.1.0" ??

DEPRECATION: An addon is trying to access project.nodeModulesPath. This is not a reliable way to discover npm modules. Instead, consider doing: require("resolve").sync(something, { basedir: project.root }). Accessed from:   new NPMDependencyVersionChecker (C:\dev\ember-cli-blog\node_modules\ember-cli-active-link-wrapper\node_modules\ember-cli-version-checker\src\npm-dependency-version-checker.js:11:33)

Ember 2.10 compatibility?

I have just updated an application to Ember 2.10. Switching between routes I now get this error

Assertion Failed: You modified routeHierarchy twice on <vendormax-6@component:bread-crumbs::ember649> in a single render. This was unreliable and slow in Ember 1.x and is no longer supported. See emberjs/ember.js#13948 for more details.

at the moment it is saying the breadcrumbs are the issue, but it flicks around with different components and different properties each time.

The error goes away completely if I remove all the active-links from the page, I know this is a truly unscientific way of testing but its the best I can do while poking around in the dark. I could create a test application for you?

Proposed changes, and how to structure PR's

I have a few proposed changes to submit but don't know how to structure the PR's. The main point of contention is moving main functionality to a Mixin. But here are my proposed changes;

  1. Move main functionality to a Mixin, that way this could be use in other addon components that might already provide a dropdown component
  2. Enable changing of the active class name, to solve issue #3
  3. Add in a "disabled" state/class when ALL child links are also disabled
  4. Enable changing of the link jQuery selector, in case {{link-to}} tagName is changed
  5. Move the childLinkViews build code out of didRender, apparently there are questions on how hooks are ran when defined in a Mixin but overridden in the Component (more research needed)
  6. Move away from the "active" property name, causes problems when used with other addon components (at least it did for me)

Here is a gist of my proposed changes for review. Again, I'll submit PR's after some discussion and best approach for PR structure.

https://gist.github.com/Panman8201/ddb4be922143d9681078

Support conditionals inside the wrapper

Heya,
Great addon, thanks for publishing it! I've run into a small issue though. When doing something like this:

{{#active-link}}
   {{#if some_condition}}
      {{a_link}}
   {{else}}
      {{some_other_link}}
   {{/if}}
{{/active-link}}

Then the navigation kind of breaks - sometimes the buttons are in the wrong state and don't update etc. Moving the active-link inside the condition fixes it:

   {{#if some_condition}}
      {{#active-link}}{{a_link}}{{/active-link}}
   {{else}}
     {{#active-link}} {{some_other_link}}{{/active-link}}
   {{/if}}

But it's not quite as nice. Any way you can support the former?

Ember 3.x compatibility

After upgrading to latest Ember 3.4, I'm getting:

Could not find module `ember` imported from `ember-cli-active-link-wrapper/components/active-link`

Seems like import Ember from 'ember' is no longer supported.

Error in Fastboot on 0.3.0

There was an error running your app in fastboot. More info about the error: 
 TypeError: Cannot read property 'lookup' of undefined

Seems to come from this line

Fastboot: 1.0.0-beta.9
Ember: 2.8.1

I think the fix for this might be to use didInsertElement to schedule the afterRender event?

Clarification

Just wanted to verify this was simply a cleaner/nicer way of writing the below (and that the below isn't going away)

        {{#link-to 'foo' tagName="li"}}
            <a {{bind-attr href="view.href"}}>details</a>
        {{/link-to}}

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.