Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bootstrap 4 and NavEE #26

Open
bgarrant opened this issue Mar 14, 2017 · 5 comments
Open

Bootstrap 4 and NavEE #26

bgarrant opened this issue Mar 14, 2017 · 5 comments

Comments

@bgarrant
Copy link

bgarrant commented Mar 14, 2017

How can we use NavEE with the new Bootstrap 4 syntax https://v4-alpha.getbootstrap.com/components/navbar/? All looks pretty easy other than the classes on the children. How can we get that working?

  <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <a class="navbar-brand" href="#">Navbar</a>
  <div class="collapse navbar-collapse" id="navbarNavDropdown">
    <ul class="navbar-nav">
      <li class="nav-item active">
        <a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Features</a>
      </li>
      <li class="nav-item">
        <a class="nav-link" href="#">Pricing</a>
      </li>
      <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle" href="http://example.com" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
          Dropdown link
        </a>
        <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
          <a class="dropdown-item" href="#">Action</a>
          <a class="dropdown-item" href="#">Another action</a>
          <a class="dropdown-item" href="#">Something else here</a>
        </div>
      </li>
    </ul>
  </div>
</nav>

this is what I have so far but dropdowns are not working

<nav class="navbar navbar-toggleable-md navbar-light bg-faded">
  <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <a class="navbar-brand" href="{{ siteUrl }}">{{ siteName }}</a>

  <div class="collapse navbar-collapse" id="navbarNavDropdown">

    {% set navConfig = {
      'startwithActive' : true,
      'maxDepth' : 2,
    } %}

    {% set navigation = craft.navee.getNav('mainMenu', navConfig) %}

    <ul class="navbar-nav">
        {% nav node in navigation %}
            <li class="nav-item{% if node.hasDescendants() %} dropdown{% endif %}">
              {% if node.hasDescendants() %}
              <a href="{{ node.link }}" class="nav-link dropdown-toggle" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">{{ node.title }}</a>
              {% else %}
              <a href="{{ node.link }}" class="nav-link">{{ node.title }}</a>
              {% endif %}

              {% ifchildren %}
              <ul class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
                {% children %}
              </ul>
              {% endifchildren %}
            </li>
        {% endnav %}
    </ul>

  </div><!--/.navbar-collapse -->
</nav>
@bgarrant
Copy link
Author

I did get it working with this...but is there a cleaner way to write the code?

<nav class="navbar navbar-toggleable-md navbar-light bg-faded">
  <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <a class="navbar-brand" href="{{ siteUrl }}">{{ siteName }}</a>

  <div class="collapse navbar-collapse" id="navbarNavDropdown">

    {% set navConfig = {
      'startwithActive' : true,
      'maxDepth' : 2,
    } %}

    {% set navigation = craft.navee.getNav('mainMenu', navConfig) %}

    <ul class="navbar-nav">
        {% nav node in navigation %}
            <li class="nav-item{% if node.hasDescendants() %} dropdown{% endif %}">
              {% if node.hasDescendants() %}
              <a href="{{ node.link }}" class="nav-link dropdown-toggle" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">{{ node.title }}</a>
              {% else %}
              <a href="{{ node.link }}" class="nav-link">{{ node.title }}</a>
              {% endif %}

              {% ifchildren %}
              <ul class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
                {% children %}
              </ul>
              {% endifchildren %}
            </li>
        {% endnav %}
    </ul>

  </div><!--/.navbar-collapse -->
</nav>

@bgarrant
Copy link
Author

How can I style the Navbar dropdown elements in this format? I need to turn this...

              {% ifchildren %}
              <ul class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
                {% children %}
              </ul>
              {% endifchildren %}

Into this...

        <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
          <a class="dropdown-item" href="#">Action</a>
          <a class="dropdown-item" href="#">Another action</a>
          <a class="dropdown-item" href="#">Something else here</a>
        </div>

@bgarrant
Copy link
Author

bgarrant commented Mar 31, 2017

Here is how you do it everyone. Bootstrap 4 only allows a 2 level menu so make sure and set Navee to max levels 2 in config.

<nav class="navbar navbar-toggleable-md navbar-light bg-faded">
  <div class="logo"><img src="/assets/layout/logo.png"></div>
  <button class="navbar-toggler navbar-toggler-right" type="button" data-toggle="collapse" data-target="#navbarNavDropdown" aria-controls="navbarNavDropdown" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <a class="navbar-brand invisible" href="{{ siteUrl }}">{{ siteName }}</a>

  <div class="collapse navbar-collapse" id="navbarNavDropdown">

    {% set navConfig = {
      'startwithActive' : true,
      'maxDepth' : 2,
    } %}

    {% set navigation = craft.navee.getNav('mainNavigation', navConfig) %}

    <ul class="navbar-nav ml-auto">
      {% for node in navigation %}
        <li class="nav-item{% if node.hasDescendants() %} dropdown{% endif %}">
          {% if node.hasDescendants() and node.level == 1 %}
          <a href="{{ node.link }}" class="nav-link dropdown-toggle" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">{{ node.title }}</a>
            <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
              {% set subNodes = node.getChildren() %}
              {% for subNode in subNodes %}
                <a class="dropdown-item" href="{{ subNode.link }}">{{ subNode.title }}</a>
              {% endfor %}
            </div>
          {% elseif node.level == 1 %}
            <a href="{{ node.link }}" class="nav-link">{{ node.title }}</a>
          {% endif %}
        </li>
      {% endfor %}
    </ul>

  </div><!--/.navbar-collapse -->
</nav>

@adamhopkinson
Copy link

Useful, thanks. Could do with some detail in the wiki regarding iterating through children/descendants.

@chukura
Copy link

chukura commented Jan 3, 2018

Indeed, it would be great if @michaelfromtheoutfit could flesh this subject out a little more in the wiki. Thank you @bgarrant, this was exactly what I was looking for.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants