4.1.0 • Published 10 months ago

mjml-custom-component-decorator v4.1.0

Weekly downloads
282
License
MIT
Repository
github
Last release
10 months ago

mjml-custom-component-decorator

npm version Continuous Test, Build & Release Renovate Quality Gate Status Reliability Rating Maintainability Rating

Features

  • Define custom MJML components with TypeScript decorator
  • Automatic discovery and registration on file load

Requirements

Installation

npm i --save mjml-custom-component-decorator

Usage

TypeScript

First of all, you will need to enable experimental decorators in your tsconfig:

{
  "compilerOptions": {
    "experimentalDecorators": true
  }
}

Then you can use it as simple as this:

import {BodyComponent, registerComponent} from "mjml-core";
import {registerDependencies} from "mjml-validator";

import {MJMLCustomComponent} from "mjml-custom-component-decorator";

@MJMLCustomComponent({
    tag: "my-custom-component",
    attributes: {
        text: {
            type: 'string',
            default: 'Hello World'
        }
    },
    allowedParentTags: ["mj-column"],
})
export class MyCustomComponent extends BodyComponent {
    render() {
        return this.renderMJML(`
            <mj-text>
                Hello there, this is what you gave as text attribute: ${this.getAttribute("text")}
            </mj-text>`
        )
    }
}

Motivation

When you are developing custom components in mjml you will find yourself writing the same boilerplate code over and over again. It gets even worse, because you need to define some static properties without any kind of autocomplete.

This is where typescript and its decorator features come handy. You write the decorator, and the project simply intercepts the class and adds the required fields. To make it even easier, TypeScript enables you to use the autocomplete of your favourite IDE, isn't that awesome?!

Just for completeness without the decorator ...

... it looks like this:

import {registerDependencies} from "mjml-validator"
import {registerComponent} from "mjml-core";

export class MyCustomComponent extends BodyComponent {
    static allowedAttributes = {
        text: 'string'
    }

    static defaultAttributes = {
        'text': 'Hello World'
    }

    static endingTag = false

    render() {
        return this.renderMJML(`
            <mj-text>
                Hello there, this is what you gave as text attribute: ${this.getAttribute("text")}
            </mj-text>`
        )
    }
}

registerComponent(MyCustomComponent)
registerDependencies({
    'mj-column': ['my-custom-component'],
    'my-custom-component': []
})

You duplicate your attribute name twice and can't have some kind of defaults (excerpt using inheritance)

Contributing

I love your input! I want to make contributing to this project as easy and transparent as possible, whether it's:

  • Reporting a bug
  • Discussing the current state of the configuration
  • Submitting a fix
  • Proposing new features
  • Becoming a maintainer

To get started please read the Contribution Guidelines.

Development

Requirements

Test

npm run test

Build

npm run build
4.1.0

10 months ago

4.0.0

2 years ago

3.2.0

2 years ago

3.1.0

2 years ago

3.0.0

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

1.2.0

3 years ago

1.1.1

3 years ago

1.1.2

3 years ago

1.1.0

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago

0.0.3

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago