0.0.98 • Published 5 years ago

@erect/static v0.0.98

Weekly downloads
-
License
MIT
Repository
gitlab
Last release
5 years ago

Erect Static

Enables Erect to render static html pages, it's particularly simple to render any template engine - as long as it can render in both Node and browser.

Usage

Basic

import '@erect/static';
import { mount } from '@erect/core';

mount(module, req => {
  return req.renderStatic(`
    <head>
      <title>Hello World</title>
    </head>
    <h1>Hello World</h1>
  `);
});

With elements

# ./shared/app.ts
import '@erect/static';
import { mount } from '@erect/core';

mount(module, (req) => {
  return req.renderStatic(req.html `
    ${Header({ title: 'Hello World' })}
    <p>Go ${Link({ href: '/about' }) `Home`}</p>
  `);
});
# ./shared/elements/Link.ts
import { element, attributes } from '@erect/static';

export interface LinkProps {
  button: boolean;
  href: string;
  target: string;
  'class': string;
  style: string; 
  children: string;
}

export const Link = element<LinkProps>((req, props) => {
  const {
    children,
    button = false,
    ...rest,
  } = props;
  
  const attr = attributes(rest);
  
  if (button) {
    attr.addClass('my-button');
    attr.addStyle({ backgroundColor: 'transparent' });
  }
 
  return req.html(([ a ]) => {
    a.addEventListener('click', (e) => {
      e.preventDefault();
      req.app.pushState(e.getAttribute('href'));
    });
  }) `
    <a${attr}>${children}</a>
  `;
});
# ./shared/elements/Indexts
import { element, attributes } from '@erect/static';

export interface HeaderProps {
  title: string;
  headline?: string;
  children: string;
  [attr: string]: any;
}

export const Header = element<HeaderProps>((req, props) => {
  const {
    children,
    button = false,
    ...rest,
  } = props;
  
  const attr = attributes()
    .combine({
      class: 'top-nav',
      style: {
        fontSize: '12px',
      },
    })
    .combine(rest);
 
  return req.html `
    <head>
      <title>${title}</title>
    </head>
    <header${attr}>
      <h1>${title}</h1>
      ${headline ? req.html `<h4>${headline}</h4>` : ''}
    </header>
  `;
});

How it works

The module extends Erect's RenderRequests class adding two new methods

req.renderStatic(html: string)

It renders the html string to the app body, if a head is present, the head elements are pushed to the app's DocumentState

req.html(strings: TemplateStringsArray, ...substitutions: any[]): string

Parses multiple html strings and append heads to the app's DocumentState

Example
req.renderStatic(
  req.html `
    <head>
      <title>Hello World</title>
    </head>
    <h1>Hello World</h1>
    ${req.html `
      <head>
        <style>.body { background-color: #f0f0f0 }</style>
      </head>
      <p>Another html with it's own head</p>
    `}
  `,
);

req.html(handler: (rootElements: HTMLElement[], rootNodes: Node[]) => void)): ((strings: TemplateStringsArray, ...substitutions: any[]) => string)

The same as above, with the exception that you can pass a handler to catch rendered root elements after mounting on browser.

Example
req.renderStatic(
  req.html(([ div, p1, p2 ]) => {
    const link = <HTMLElement>div.firstElementChild;
    link.addEventListener('click', () => {
      alert('clicked');
      return false;
    });
    p1.style.backgroundColor = 'red';
    p2.style.backgroundColor = 'green';
  }) `
    <head>
      <title>Hello World</title>
    </head>
    <div>
      <a href="">click here</a>
    </div>
    <p>First paragraph</p>
    <p>Second paragraph</p>
  `,
);

element

Creates a template element / partial

Example
export interface CodeProps {
  children: string;
}
export const Code = element<CodeProps>((req, props) => {
  return req.html `<pre><code>${props.children}</code></pre>`;
});
0.0.98

5 years ago

0.0.97

5 years ago

0.0.96

5 years ago

0.0.95

5 years ago

0.0.94

5 years ago

0.0.93

5 years ago

0.0.92

5 years ago

0.0.91

5 years ago

0.0.90

5 years ago

0.0.89

5 years ago

0.0.88

5 years ago

0.0.87

5 years ago

0.0.86

5 years ago

0.0.85

5 years ago

0.0.84

6 years ago

0.0.83

6 years ago

0.0.82

6 years ago

0.0.81

6 years ago

0.0.80

6 years ago

0.0.79

6 years ago

0.0.78

6 years ago

0.0.77

6 years ago

0.0.76

6 years ago

0.0.75

6 years ago

0.0.74

6 years ago

0.0.73

6 years ago

0.0.72

6 years ago

0.0.71

6 years ago

0.0.70

6 years ago

0.0.69

6 years ago

0.0.68

6 years ago

0.0.67

6 years ago

0.0.66

6 years ago

0.0.65

6 years ago

0.0.64

6 years ago

0.0.63

6 years ago

0.0.62

6 years ago

0.0.61

6 years ago

0.0.9

6 years ago

0.0.8

6 years ago

0.0.7

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago