framework icon indicating copy to clipboard operation
framework copied to clipboard

Feature request: Components for Common HTML

Open mattycraig opened this issue 7 years ago • 8 comments

  • Maizzle Version: 0.1.1
  • Node.js Version: 11.11.0

Description:

Hi there! Please excuse me if this is in the wrong format.

I want to say thank you for all your hard work in putting this together. I love the speed & ease I can pump out email templates using Maizzle + Tailwind.

I wanted to suggest giving users the option to use prebuilt Nunjucks components to avoid wewriting out painfully tedious HTML table markup (and similar elements).

I've created a some basic prop-based components while using the {{ cotnent }} slot for reusability (as per the docs) but also to allow for nesting components.

Table Component Examples

Single <tr> and <td>

<!-- src/components/table-singletd.njk -->

<table class="{{ data.classTb }}">
  <tr class="{{ data.classTr }}">
    <td class="{{ data.classTd }}">
      {{ content }}
    </td>
  </tr>
</table>

Multiple nested <tr> inside of {{ content }}

<!-- src/components/table-multitr.njk -->

<table class="{{ data.classTb }}">
  {{ content }}
</table>

Single <tr> with multiple nested <td> inside of {{ content }}

<!-- src/components/table-multitd.njk -->

<table class="{{ data.classTb }}">
  <tr class="{{ data.classTr }}">
    {{ content }}
  </tr>
</table>

Other Exmples

Simple anchor tag with conditional check to see if href is empty.

<!-- src/components/anchor.njk -->

<a {% if data.href %} href="{{data.href}}"{% endif %}>
  {{ content }}
</a>

Using Components in Templates

In our Templates we can now use the component as we like using the instructions to the Maizzle docs.

However I chose to set each component to a variable for sake of not repeating the path multiple times (should the components folder be moved, etc).

This markup...

<!-- src/templates/my-template.njk -->

{% set multiTd = "src/components/table-multitd.njk" %}
{% set a = "src/components/anchor.njk" %}

{% component multiTd, { classTb: 'w-full' }% }
  <td class="w-1-2">
    { %component a, { class: 'btn-link', href: 'http://www.maizzle.com'} %}
      Maizzle Home
    {%endcomponent%}
  </td>
  <td class="w-1-2">
    {% component a, { class: 'btn-link', href: '#'} %}
      Product
    {% endcomponent %}
  </td>
{% endcomponent %}

Will output this when compiled...

<table class="w-full bg-gray-900" cellpadding="0" cellspacing="0" role="presentation">
  <tbody>
    <tr>
      <td class="w-1-2">
        <a class="text-orange-100" href="http://www.maizzle.com">
          Maizzle Home
        </a>
      </td>
      <td class="w-1-2">
        <a class="text-orange-100">
          Other Anchor (No href prop specified)
        </a>
      </td>
    </tr>
  </tbody>
</table>

I figure this could be explanded into much more such as a columned artciles, hero areas, reusable buttons, social media buttons, etc, etc.

Let me know if this is something you'd worth considering into the framework and I will happily make a PR with a few different reusable components.

Thanks!

mattycraig avatar Apr 14 '19 04:04 mattycraig

Hi, and thanks for the kind words! :)

I completely agree it would be cool to have ready-made components, and the good news is I'm already working on something for this. It will be separate from the framework though, but still easy to pull in. And you will be able to contribute your own components, of course ;)

It's still in its early stages, I'll announce on Twitter once I have a basic working version.

cossssmin avatar Apr 15 '19 10:04 cossssmin

since this post has been a great source of inspiration in my maizzle learning, I was hoping one of you may have a suggestion.

I am trying to use IF / ELSIF / ELSE in a component. I'd like to define it in front matter or SET it at the top of the document.

This is to avoid duplicating a footer to change some small parts of it.

Any ideas? I've been going round in circles for a few days off and on. At most I can display IF and ELSE but not ELSIF.

Thanks!

daniel-thisnow avatar Oct 16 '19 21:10 daniel-thisnow

Can you share your code, Daniel?

Note: Nunjucks' syntax is elif or elseif, not elsif.

cossssmin avatar Oct 17 '19 12:10 cossssmin

Hi Cosmin, I settled on passing an argument with the data variable. I was going a little delirious when I posted before.

`{% component example,{

  option1: '',
  option2: 'bu == true',
  option3: ''


   } %}`

Then inside the component I have

{% if data.option1 %} option1 {% elif data.option2 %} option2 {% else %} option3 {% endif %}

Its a bit crude, but it works. The option I wan't either resolves to true or defaults to else. It would be great to work out how to set it at the top of the document, but it does give a bit more control for variations that don't fit.

daniel-thisnow avatar Oct 18 '19 08:10 daniel-thisnow

Getting back to this...

I initially wanted something like Foundation did with Blocks, where you could have a CLI command that would pull in a specific component.

The more I think about it, the more I don't like the idea.

It sounds cool, but I feel like such a workflow wouldn't be very easy to use, and you'd probably end up wanting to browse components on a website and copy-paste them into your code.

Sooo, I think that I'll create a repo with some common layout patterns instead, like responsive 3 column layouts or fluid-hybrid 2 column layouts. Easy to browse, search, and get code from. And people could contribute, of course. They'd be HTML-only, I wouldn't want to pick what data can be passed to them, as use-cases differ.

We could probably also add a new CLI command that would download all of these into your ./src/components project directory.


For now, we have a bunch of starters (with more on the way!). But yeah, some common layout patterns would be useful. Hope to get this one out in the first half of the year...

cossssmin avatar Jan 22 '20 15:01 cossssmin

What about doing something similar to HEML where you abstract away the repetitive markup into reusable components such as <row> and <column>? https://heml.io/docs/elements/layout

stursby avatar Apr 30 '20 16:04 stursby

Since migrating to PostHTML, I've been thinking about a PostHTML plugin that would let you define mappings containing custom tags and what they would expand to.

This isn't a priority right now and would definitely be a PostHTML plugin if used in Maizzle, but otherwise I'm open to the idea. What I really don't want is to make a HEML/MJML clone, so it would probably not be that complex anyway.

cossssmin avatar Apr 30 '20 17:04 cossssmin

@cossssmin sounds good! Thanks for the input.

stursby avatar Apr 30 '20 18:04 stursby