Skip to content

substrate-system/email

Repository files navigation

email

tests types module install size GZip size semantic versioning Common Changelog license

Web component for email inputs.

featuring:

  • robust client-side validation -- check that email is of the form abc@domain.tld
  • good UX for validation
    • errors are only shown if the input has been focused
    • errors are not shown until the input blurs
  • add a class to the element when it is not valid
  • emit valid and invalid events when validity changes

See a live demo

Install

npm i -S @substrate-system/email

Modules

This exposes ESM and common JS via package.json exports field.

ESM

import { email } from '@substrate-system/email'

Common JS

const { email } = require('@substrate-system/email')

Example

See ./example, and the demo page.

import { SubstrateEmail } from '@substrate-system/email'
import { SubstrateButton } from '@substrate-system/button'
SubstrateEmail.define()
SubstrateButton.define()
const qs = document.querySelector

const input = qs('form substrate-email')
input?.addEventListener('valid', ev => {
    console.log('We are valid!', ev)
    qs('substrate-button')!.disabled = false
})

input?.addEventListener('invalid', ev => {
    console.log('no longer valid....', ev)
    qs('substrate-button')!.disabled = true
})

CSS

Bundler

The package.json exposes css, suitable for vite or esbuild:

import '@substrate-system/email/css'

Or minified:

import '@substrate-system/email/css/min'

CSS imports

If using a CSS processor, you can import from the CSS files:

@import url("../node_modules/@substrate-system/email/dist/index.min.css");

Customize CSS via some variables

substrate-email {
    --bgc: #fafafa;
    --color: black;
    --focus: #005fcc;
    --error-color: red;
}

Use

You can set a name for this custom element with the static method define. To use the default name, substrate-email, just import and call .define().

Caution

If you change the name of the web component, it will break the CSS.

import { SubstrateEmail } from '@substrate-system/email'

// create a web component named `substrate-email`
SubstrateEmail.define()

Override the tag property to change the tag name:

import { SubstrateEmail } from '@substrate-system/email'

// set a custom name
SubstrateEmail.TAG = 'cool-input'

SubstrateEmail.define()

HTML

<div>
    <substrate-email></substrate-email>
</div>

pre-built

This package exposes minified JS and CSS files too. Copy them to a location that is accessible to your web server, then link to them in HTML.

copy

cp ./node_modules/@substrate-system/email/dist/index.min.js ./public/substrate-email.min.js
cp ./node_modules/@substrate-system/email/dist/style.min.css ./public/substrate-email.css

HTML

<head>
    <link rel="stylesheet" href="./substrate-email.css">
</head>
<body>
    <!-- ... -->
    <script type="module" src="./substrate-email.min.js"></script>
</body>

Contributors 2

  •  
  •