components.njk 16.3 KB
Newer Older
1 2 3 4 5
---
layout: layouts/base.njk
pageClass: components
templateEngineOverride: njk
---
6 7 8
{% set component = pagination.items[0] %}
{% set collectionComponents = component.items %}
{% set variants = component.variants.items %}
9

10
  <section class="vf-content component-library-notes | vf-stack vf-stack--400">
11 12 13
    {# for any content set in the page, this is most likely blank #}
    {{ content | safe }}

14
    {% if component.variants.items[0].status.label == "alpha" -%}
15
      <div class="vf-banner vf-banner--alert vf-banner--danger">
16 17 18 19 20 21 22 23 24
        <div class="vf-banner__content">
        {% set context = {
          "text": "alpha",
          "theme": "tertiary"
          } %}
        {% include "vf-core-components/vf-badge/vf-badge.njk" %}
        <p class="vf-banner__text">
        This version is in early development and is subject to change and therefore <strong>not ready for wide use</strong>. There may be other <a class="vf-banner__link" href="https://www.npmjs.com/package/@visual-framework/{{component.baseHandle}}">stable versions available</a>.
        </p></div>
25
      </div>
26 27
    {%- endif %}
    {% if component.variants.items[0].status.label == "beta" -%}
28
      <div class="vf-banner vf-banner--alert vf-banner--warning">
29 30 31 32 33 34 35
        <div class="vf-banner__content"><p class="vf-banner__text">
        {% set context = {
          "text": "beta",
          "theme": "tertiary"
          } %}
        {% include "vf-core-components/vf-badge/vf-badge.njk" %}
        This component version is ready to be used but is still in active development.</p></div>
36
      </div>
37 38
    {%- endif %}
    {% if component.variants.items[0].status.label == "deprecated" -%}
39
      <div class="vf-banner vf-banner--alert vf-banner--danger">
40 41 42 43 44 45
        <div class="vf-banner__content"><p class="vf-banner__text">
        {% set context = {
          "text": "deprecated",
          "theme": "tertiary"
          } %}
        {% include "vf-core-components/vf-badge/vf-badge.njk" %}
46
        It is strongly recommended that this <strong>not to be used</strong>.</p></div>
47
      </div>
48
    {%- endif %}
49

Ken Hawkins's avatar
Ken Hawkins committed
50 51 52 53 54 55
    {% if component.handle == "vf-design-tokens" %}
      {% markdown %}
      [View the design token docs](/design-tokens/)
      {% endmarkdown %}
    {% endif %}

56 57
    {# readme.md #}
    {% if component.notes %}
58 59 60 61 62 63 64 65
      {% if collectionComponents %}
      {% else %}
        {% set componentLinks %}
          <a class="vf-link" href="https://github.com/visual-framework/vf-core/tree/develop/components/{{component.baseHandle}}"><img src="https://img.shields.io/badge/github-{{component.baseHandle | replace("-","--")}}-blue" alt="github location" /></a>
          <a class="vf-link" href="https://badge.fury.io/js/%40visual-framework%2F{{component.baseHandle}}"><img src="https://badge.fury.io/js/%40visual-framework%2F{{component.baseHandle}}.svg" alt="npm version"></a>
        {% endset %}
      {% endif %}

66
      {% set readmeAbout = component.notes | string | stringBetween("## About", "\n## ") %}
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
      {% if readmeAbout %}
        {% set readmeAbout = readmeAbout | markdown | safe | striptags | string %}
        {% render '@vf-intro', {
          "id": "about",
          "vf_intro_heading": component.title,
          "vf_intro_badge": {
            "style": [
              "phases"
            ],
            "text": variants[0].context['component-type'],
            "theme": "primary",
            "badge_href": "/components#" + variants[0].context['component-type']
          },
          "vf_intro_subheading": "",
          "vf_intro_lede": readmeAbout.val,
          "vf_intro_text": [
            componentLinks
           ]
        } %}
      {% endif %}
87 88 89 90 91 92 93 94 95 96 97 98 99
    {% endif %}

    {# any sub-components, like vf-form #}
    {# @todo, this is still not rendering correctly #}
    {% if collectionComponents %}
      {# {% render '@vf-heading--display', {
        "type": "display",
        "heading": "There are sub-components",
        "style": "invert",
        "tags": "h1"
      } %} #}
      <ul class="vf-list vf-list--unordered">
        {%- for component in collectionComponents %}
100 101 102 103
          {% if  component.variants.isHidden ==  false %}
            <li class="vf-list__item"><a class="vf-link" href="{{ component.variants.items[0].alias }}">{{ component.variants.title }}</a> <code> {{ component.variants.items[0].alias }} </code></li>
            {#- <pre>{{ component.variants | dump(2)}}</pre> -#}
          {% endif -%}
104 105 106 107
        {% endfor -%}
      </ul>
    {% endif %}

108
    {# Table of contents #}
109
    {# {% set navigationList = [
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
      {
        "text": "Usage",
        "link_list_href": "#usage"
      },
      {
        "text": "Variants",
        "link_list_href": "#variants"
      },
      {
        "text": "Examples",
        "link_list_href": "#examples"
      },
      {
        "text": "Install",
        "link_list_href": "#install"
      },
      {
        "text": "Changelog",
        "link_list_href": "#changelog"
      },
      {
        "text": "Assets",
        "link_list_href": "#assets"
      }
    ] %}
    {% render '@vf-link-list', {
      "component_modifier": "vf-links__list--easy",
      "title": "On this page",
      "svg": true,
      "list":
        navigationList
141 142 143 144 145 146 147
    } %} #}

    {%- set useJavaScriptForOnThisPage = true -%}
    {%- if component.handle == 'vf-navigation' -%}
      {#- Don't use javascript for vf-navigation page as it conflicts with the demo -#}
      {%- set useJavaScriptForOnThisPage = false -%}
    {%- endif -%}
148

149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
    {%- set context = {
      "heading": "On this page",
      "classModifier": "on-this-page",
      "activateJavascript": useJavaScriptForOnThisPage,
      "navigation": [
        {
          "text": "Usage",
          "navigation_href": "#usage",
          "currentSection": true
        },
        {
          "text": "Variants",
          "navigation_href": "#variants"
        },
        {
          "text": "Examples",
          "navigation_href": "#examples"
        },
        {
          "text": "Install",
169
          "navigation_href": "#installation"
170 171 172 173 174 175 176 177 178 179 180 181 182
        },
        {
          "text": "Changelog",
          "navigation_href": "#changelog"
        },
        {
          "text": "Assets",
          "navigation_href": "#assets"
        }
      ]
    }
    -%}
    {%- include "vf-core-components/vf-navigation/vf-navigation.njk" -%}
183

184 185 186
    {% if component.notes %}
      {% set readmeUsage = component.notes | string | stringBetween("## Usage", "\n## ") %}
      {% if readmeUsage %}
187 188 189
        <section class="embl-grid">
          {% render '@vf-section-header', {
            "section_title": "Usage",
190
            "id": "usage",
191
            "href": ""
192
          } %}
193 194 195
          <article>
          {{ readmeUsage | markdown | safe }}
          </article>
196 197 198
        </section>
      {% endif %}
    {% endif %}
199

200 201 202
    {# ---- #}

    {% if variants %}
203 204 205 206 207 208 209
      {% render '@vf-heading--display', {
        "type": 1,
        "heading": "Variants",
        "id": "variants",
        "style": "invert",
        "tags": "h2"
      } %}
210
      {%- for variant in variants %}
211 212
        {% if variant.isHidden == false and variant.context['isExample'] != true %}
          {% if variants | length > 1  %}
213 214
            {# If more than 1 variant type, show variant specific details #}
            {% render '@vf-section-header--is-a-link', {
Ken Hawkins's avatar
Ken Hawkins committed
215
              "section_title": "" + variant.label + " variant",
216 217 218 219 220 221 222 223
              "id": variant.handle,
              "href": "#" + variant.handle
            } %}
          {% endif %}

          <article class="vf-stack vf-stack--lg vf-u-margin__bottom--1200"
                   style="clear: both; /* insurance against strange components */"
          >
224 225 226 227 228
            {% if variants[0].context['exampleMultiColumns'] != "false" and (variants[0].context['component-type'] == "block" or variants[0].context['component-type'] == "element") %}
              <section class="vf-grid vf-grid__col-3">
              <div><!--empty--></div>
            {% endif %}
              {% if component.baseHandle == 'vf-u-fullbleed' %}
229
                {% render '@'+variant.handle, variant.context %}
230 231 232 233 234 235 236 237 238 239 240
              {% else %}
                <div class="vf-u-padding--400"
                    style="overflow: auto; background-image: linear-gradient(45deg, rgba(59, 111, 182, .1) 25%, transparent 25%), linear-gradient(135deg, rgba(59, 111, 182, .1) 25%, transparent 25%), linear-gradient(45deg, transparent 75%, rgba(59, 111, 182, .1) 75%), linear-gradient(135deg, transparent 75%, rgba(59, 111, 182, .1) 75%);background-size: 20px 20px; background-position-x: 0px, 10px, 10px, 0px;background-position-y: 0px, 0px, -10px, 10px;"
                >
                  {# When you want a box with a slight shadow #}
                  {% render '@'+variant.handle, variant.context %}
                </div>
              {% endif %}
            {% if variants[0].context['exampleMultiColumns'] != "false" and (variants[0].context['component-type'] == "block" or variants[0].context['component-type'] == "element") %}
              <div><!--empty--></div>
              </section>
241 242
            {% endif %}
            {% if variant.context['component-type'] != "utility" and variant.context['component-type'] != "grid" and variant.context['component-type'] != "embl-grid" %}
243
              {# Utility and grid variants have no practical njk or react templates #}
244 245
              <details class="vf-details vf-box vf-box--outline">
                <summary class="vf-details--summary">Nunjucks syntax</summary>
246 247 248 249 250 251 252

      {% markdown %}

Depending on your environment you'll want to use `render` or `include`. As a rule of thumb: server-side use `include`, precompiled browser use `render`. If you're using [vf-eleventy](https://github.com/visual-framework/vf-eleventy) you should use `include`.

### Using `include`

Nitin Jadhav's avatar
Nitin Jadhav committed
253
You'll need to pass a context object from your code or Yaml file ([example](https://github.com/visual-framework/vf-eleventy/blob/master/src/site/index.yml)), as well as the path to the Nunjucks template. Nunjucks' `include` is an abstraction of `render` and provides some additional portability.
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277

{# Ideally we could also show the Yaml format here, but that will require additional parsing of the passed `variant.context`. Not impossible, but an additional task #}
      {% endmarkdown %}

                <pre><code>
{%- raw %}
{% set context fromYourYamlFile %}
- or -
{% set context = {% endraw %}
  {{-variant.context | dump(2)}}
{% raw %} %}
{% include "../path_to/{% endraw %}{{variant.baseHandle}}/{{variant.baseHandle}}.njk{%- raw %}" %}{% endraw %}
                </code></pre>

                {# <pre><code>
                {{-variant.context | dump(2)}}
                </code></pre> #}
      {% markdown %}

### Using `render`

This approach is best for bare-bones Nunjucks environments, such as precompiled templates with the Nunjucks slim runtime where `include` is not be available.

      {% endmarkdown %}
278
                <pre><code>
279
                {%- raw %}{% render '@{% endraw %}{{variant.baseHandle}}', {{variant.context | dump(2)}} {% raw %}%}{% endraw %}
280 281
                </code></pre>
              </details>
Nitin Jadhav's avatar
Nitin Jadhav committed
282 283
              {% set reactComponents = "vf-card, vf-button, vf-back-to-top" %}
              {% if component.baseHandle in reactComponents %}
284
              <details class="vf-details vf-box vf-box--outline">
285
                <summary class="vf-details--summary">React syntax (pre-alpha)</summary>
286
                <pre><code>
Nitin Jadhav's avatar
Nitin Jadhav committed
287 288 289 290
import { {{ component.baseHandle | camelize(true) | replace("-", "") }} } from "@visual-framework/{{component.baseHandle}}/{{component.baseHandle}}.react.js";
// or
import { {{ component.baseHandle | camelize(true) | replace("-", "") }} } from "@visual-framework/{{component.baseHandle}}/{{component.baseHandle}}.jsx";
{{ '<' | escape }}{{ component.baseHandle | camelize(true) | replace("-", "") }} parameter="value" />
291
                </code></pre>
292
                <br/><p>For individual parameter names and options, see the Nunjucks syntax example. <a href="https://github.com/visual-framework/vf-core/blob/develop/tools/vf-extensions-react/README.md">Also follow the React setup guide</a>. Note: React support is in its early pre-alpha stage and not all component are yet supported.</p>
293
              </details>
294
              {% endif %}
295 296 297 298 299 300 301 302 303 304
            {% endif %}

            {# Show and escaped version of the html #}
            <details class="vf-details vf-box vf-box--outline">
              <summary class="vf-details--summary">HTML</summary>
              <pre>
                {%- render '@'+variant.handle, variant.context, false, { escape: false, beautify: true, highlight: true } %}
              </pre>
            </details>
          </article>
305 306 307 308
        {% endif %}

      {% endfor -%}

309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
      {# Examples #}
      {% render '@vf-heading', {
        "type": 1,
        "heading": "Examples",
        "id": "examples",
        "style": "invert",
        "tags": "h2"
      } %}
      {% if variants[0].context['component-type'] == "block" or variants[0].context['component-type'] == "element" %}
      <section class="{% if variants[0].context['exampleMultiColumns'] != "false" %}vf-grid vf-grid__col-3{% else %}{% endif %}">
      {% endif %}
      {%- for variant in variants %}
        {% if variant.isHidden == false and variant.context['isExample'] == true %}
          {# {% if component.baseHandle == 'vf-u-fullbleed' %} #}
          {% if variants[0].context['component-type'] == "block" or variants[0].context['component-type'] == "element" %}
            <section>
              {% render '@vf-section-header', {
                "section_title": variant.label,
                "id": variant.handle,
                "href": "#" + variant.handle
              } %}
              <article class="vf-u-padding--400"
331
                  style="background-image: linear-gradient(45deg, rgba(59, 111, 182, .1) 25%, transparent 25%), linear-gradient(135deg, rgba(59, 111, 182, .1) 25%, transparent 25%), linear-gradient(45deg, transparent 75%, rgba(59, 111, 182, .1) 75%), linear-gradient(135deg, transparent 75%, rgba(59, 111, 182, .1) 75%);background-size: 20px 20px; background-position-x: 0px, 10px, 10px, 0px;background-position-y: 0px, 0px, -10px, 10px;"
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
              >
                {# When you want a box with a slight shadow #}
                {% render '@'+variant.handle, variant.context %}
              </article>
            </section>
          {% else %}
            <article class="vf-u-padding--400" style="
              overflow: scroll;
              margin: 0;
              padding: 0;
            ">
              {% render '@'+variant.handle, variant.context %}
            </article>
          {% endif %}
        {% endif %}
      {% endfor -%}
      {% if variants[0].context['component-type'] == "block" or variants[0].context['component-type'] == "element"%}
      </section>
      {% endif %}

352 353
      {% markdown %}

354 355 356 357 358 359 360 361 362 363
      {% if component.notes %}
      {% set readmeInstallation = component.notes | string | stringBetween("## Install", "\n## ") %}

      {% if readmeInstallation %}
      <details class="vf-details">
        <summary class="vf-details--summary" id="installation">Installation info</summary>
        {{ readmeInstallation | replace("## Install", "") | markdown | safe }}
      </details>
      {% endif %}

364
      {% if 'CHANGELOG.md' | length %}
365 366 367 368 369 370
      {% markdown %}
      <div class="vf-stack vf-stack--custom" style="--vf-stack-margin--custom: .5rem;">
      <h3>Changelog</h3>
      <details class="vf-details" id="changelog">
        <summary class="vf-details--summary">Changelog</summary>

371
        {% include 'CHANGELOG.md' ignore missing %}
372 373 374 375 376 377 378 379 380 381 382 383 384
      </details>
      </div>
      {% endmarkdown %}
      {% endif %}

    {% else %}
      {% markdown %}
        {% if collectionComponents %}
        <p>This is a collection of components.</p>
        {% endif %}
      {% endmarkdown %}
    {% endif %}

385
      ### Assets <span id="assets"></span>
386 387 388 389 390 391 392 393 394 395 396

      - [Nunjucks template]({{ ('/assets/' + component.baseHandle + '/' + component.baseHandle + '.njk') | url }})
      - [Config]({{ ('/assets/' + component.baseHandle + '/' + component.baseHandle + '.config.yml') | url }})

      {%- for asset in component.resources.assets %}
      - [{{asset.base}}]({{ ('/assets/' + asset.relPath ) | url }})
      {%- endfor %}

      {% endmarkdown %}
    {% endif %}

397 398 399
    {% if collectionComponents %}
    {% else %}
      {% markdown %}
400

401
        ---
402

403
        <br/>
404

405 406 407
        <p class="vf-text-body vf-text-body--4">
        File system location: <code>components/{{ component.baseHandle }}</code>
        </p>
408

409 410
      {% endmarkdown %}
    {% endif %}
411 412

  </section>