49.0.0 • Published 3 years ago

@erickmerchant/framework v49.0.0

Weekly downloads
422
License
ISC
Repository
github
Last release
3 years ago

@erickmerchant/framework

A simple frontend framework.

Get started

import {
  cache
  createApp,
  html,
} from 'https://cdn.skypack.dev/@erickmerchant/framework?min'

const app = createApp({items: []})

createApp takes the initial state of the app. It can be any type, but an object usually makes sense.

Rendering some html

const target = document.querySelector('#app');

const cls = getCls();

const view = (state) => html`
  <div id="app" class=${cls} />
`;

app.render(view, target);

This doesn't do much right now, but it does demonstrate a few things.

  • How to render an element with no children. The self closing /> is required even on tags that normally wouldn't need it and it's allowed on all, even those that normally wouldn't allow it.
  • How to render static attributes. id="app" is static. It will be the same each time this view is rendered. The quotes (single or double) are require.
  • How to render cached attributes. class=${cls} is cached. Its value is not hard-coded. However every time the view is rendered it will not change, even if getCls() returns a new value.

Dynamic children

const view = (state) => html`
  <div id="app">
    <ol>
      ${state.items.map(
        (item) =>
          html`
            <li>${item}</li>
          `
      )}
    </ol>
  </div>
`;

This shows how you can have dynamic children and how you'd output an array of items.

Cached children

Sometimes you'll have some html that needs to be dynamic once but after that can be treated as if it were static. That's where cache comes into play.

const title = 'The heading';

const heading = cache(
  html`
    <h1>${title}</h1>
  `
);

const view = (state) => html`
  <div id="app">
    ${heading}
    <ol>
      ${state.items.map(
        (item) =>
          html`
            <li>${item}</li>
          `
      )}
    </ol>
  </div>
`;

Properties

Cached attributes are useful, but often you'll have attributes that need to change. This is where you can use properties instead. Properties are always reevaluated. You can indicate a property with :, a single colon.

Please note that class will get expanded to className and for will get expanded to htmlFor.

let hasFoo = true;

let barValue = "I'm the bar value";

const view = (state) => html`
  <form id="app">
    <label>
      Has foo
      <input type="checkbox" :checkbox=${hasFoo} />
    </label>
    <label>
      Bar value
      <input type="text" :value=${barValue} />
    </label>
  </form>
`;

Also worth pointing out in this example is that hasFoo is a boolean, which demonstrates how to render boolean properties.

Events

const onClick = (e) => {
  e.preventDefault();
};

const view = (state) => html`
  <div id="app">
    <button @click=${onClick}>Click here</button>
  </div>
`;

The framework always uses event delegation. For instance with this click handler above a single event handler is added to the document with capture set to true. When a click occurs the target is checked to see if it was registered as having a handler. If it was then the handler is called with the event object.

Changing state

const onClick = (e) => {
  e.preventDefault();

  app.state = {count: app.state.count + 1};
};

const view = (state) => html`
  <div id="app">
    <p>${state.count}</p>
    <button @click=${onClick}>Click here</button>
  </div>
`;

Server-side rendering

import {stringify, html} from '@erickmerchant/framework/stringify.js';

const view = (state) => html`
  <div id="app">
    <ol>
      ${state.items.map(
        (item) =>
          html`
            <li>${item}</li>
          `
      )}
    </ol>
  </div>
`;

const staticHTML = stringify(view({items}));

res.write(staticHTML);
49.0.0

3 years ago

48.4.0

3 years ago

48.3.0

3 years ago

48.2.0

3 years ago

48.1.1

3 years ago

48.1.0

3 years ago

48.0.3

3 years ago

48.0.2

3 years ago

48.0.1

3 years ago

48.0.0

3 years ago

47.1.0

3 years ago

47.0.0

3 years ago

46.0.0

3 years ago

45.0.0

3 years ago

44.5.0

3 years ago

44.4.1

3 years ago

44.4.0

3 years ago

44.3.0

3 years ago

44.3.1

3 years ago

44.2.0

3 years ago

44.1.0

3 years ago

44.0.0

3 years ago

43.9.0

3 years ago

43.7.0

3 years ago

43.8.0

3 years ago

43.6.0

3 years ago

42.1.0

3 years ago

43.0.0

3 years ago

42.2.1

3 years ago

42.2.0

3 years ago

43.5.0

3 years ago

43.3.1

3 years ago

43.3.0

3 years ago

43.4.0

3 years ago

43.1.2

3 years ago

43.1.1

3 years ago

43.1.0

3 years ago

43.2.0

3 years ago

42.0.1

3 years ago

42.0.0

3 years ago

41.8.0

3 years ago

41.7.1

3 years ago

41.7.0

3 years ago

41.6.0

3 years ago

41.5.2

3 years ago

41.5.1

3 years ago

41.5.0

3 years ago

41.4.0

3 years ago

41.3.0

3 years ago

41.2.2

3 years ago

41.2.1

3 years ago

41.2.0

3 years ago

41.1.0

3 years ago

41.0.1

3 years ago

41.0.0

3 years ago

40.3.0

3 years ago

40.2.2

3 years ago

40.2.1

3 years ago

40.2.0

3 years ago

40.1.1

3 years ago

40.1.0

4 years ago

40.0.0

4 years ago

39.3.1

4 years ago

39.3.0

4 years ago

39.2.0

4 years ago

39.1.0

4 years ago

39.0.1

4 years ago

39.0.0

4 years ago

38.0.0

4 years ago

37.1.0

4 years ago

37.0.1

4 years ago

37.0.0

4 years ago

36.0.1

4 years ago

36.0.0

4 years ago

35.2.0

4 years ago

35.1.3

4 years ago

35.1.2

4 years ago

35.1.1

4 years ago

35.1.0

4 years ago

35.0.0

4 years ago

34.0.0

4 years ago

33.0.0

4 years ago

32.3.3

4 years ago

32.3.2

4 years ago

32.3.1

4 years ago

32.3.0

4 years ago

32.2.3

4 years ago

32.2.2

4 years ago

32.2.1

4 years ago

32.2.0

4 years ago

32.1.2

4 years ago

32.1.1

4 years ago

32.1.0

4 years ago

32.0.1

4 years ago

32.0.0

4 years ago

31.0.2

4 years ago

31.0.0

4 years ago

31.0.1

4 years ago

30.0.1

4 years ago

30.0.0

4 years ago

29.2.3

4 years ago

29.2.0

4 years ago

29.2.1

4 years ago

29.2.2

4 years ago

29.1.1

4 years ago

29.1.0

5 years ago

29.0.0

5 years ago

28.0.2

5 years ago

28.0.1

5 years ago

28.0.0

5 years ago

27.1.0

5 years ago

27.0.0

5 years ago

26.0.1

5 years ago

26.0.0

5 years ago

25.1.0

5 years ago

25.0.0

5 years ago

24.6.1

5 years ago

24.6.0

5 years ago

24.5.0

5 years ago

24.4.6

5 years ago

24.4.5

5 years ago

24.4.4

5 years ago

24.4.3

5 years ago

24.4.2

5 years ago

24.4.1

5 years ago

24.4.0

5 years ago

24.3.1

5 years ago

24.3.0

5 years ago

24.2.0

5 years ago

24.1.1

5 years ago

24.1.0

5 years ago

24.0.0

5 years ago

23.1.1

5 years ago

23.1.0

5 years ago

23.0.3

5 years ago

23.0.2

5 years ago

23.0.1

5 years ago

23.0.0

5 years ago

22.7.1

5 years ago

22.7.0

5 years ago

22.6.0

5 years ago

22.5.1

5 years ago

22.5.0

5 years ago

22.4.0

5 years ago

22.3.0

5 years ago

22.2.2

5 years ago

22.2.1

5 years ago

22.2.0

5 years ago

22.1.1

5 years ago

22.1.0

5 years ago

22.0.3

5 years ago

22.0.2

5 years ago

22.0.1

5 years ago

22.0.0

5 years ago

21.1.2

5 years ago

21.1.1

5 years ago

21.1.0

5 years ago

21.0.0

5 years ago

20.3.3

5 years ago

20.3.2

5 years ago

20.3.1

5 years ago

20.3.0

5 years ago

20.2.0

5 years ago

20.1.1

5 years ago

20.1.0

5 years ago

20.0.0

5 years ago

19.2.0

5 years ago

19.1.0

5 years ago

19.0.0

5 years ago

18.0.0

5 years ago

17.1.2-3

5 years ago

17.1.2-2

5 years ago

17.1.2-1

5 years ago

17.1.2-0

5 years ago

17.1.1

6 years ago

17.1.0

6 years ago

17.0.0

6 years ago

16.0.1

6 years ago

16.0.0

6 years ago

15.0.0

6 years ago

14.1.4

6 years ago

14.1.3

6 years ago

14.1.2

6 years ago

14.1.1

6 years ago

14.1.0

6 years ago

14.0.0

6 years ago

13.3.2

6 years ago

13.3.1

6 years ago

13.3.0

6 years ago

13.2.0

6 years ago

13.1.2

6 years ago

13.1.1

6 years ago

13.1.0

6 years ago

13.0.1

7 years ago

13.0.0

7 years ago

12.2.1

7 years ago

12.2.0

7 years ago

12.1.2

7 years ago

12.1.1

7 years ago

12.1.0

7 years ago

12.0.2

7 years ago

12.0.1

7 years ago

12.0.0

7 years ago

11.0.1

7 years ago

11.0.0

7 years ago

10.0.3

7 years ago

10.0.2

7 years ago

10.0.1

7 years ago

10.0.0

7 years ago

9.2.2

7 years ago

9.2.1

7 years ago

9.2.0

7 years ago

9.1.0

7 years ago

9.0.3

7 years ago

9.0.2

7 years ago

9.0.1

7 years ago

9.0.0

7 years ago

8.0.0

7 years ago

7.8.1

7 years ago

7.8.0

7 years ago

7.7.1

7 years ago

7.7.0

7 years ago

7.6.0

7 years ago

7.5.0

7 years ago

7.4.0

7 years ago

7.3.0

7 years ago

7.2.0

7 years ago

7.1.0

7 years ago

7.0.0

8 years ago

6.1.0

8 years ago

6.0.0

8 years ago

5.0.1

8 years ago

5.0.0

8 years ago

4.0.1

8 years ago

4.0.0

8 years ago

3.1.0

8 years ago

3.0.1

8 years ago

3.0.0

8 years ago

2.0.0

8 years ago

1.5.0

8 years ago

1.4.0

8 years ago

1.3.0

8 years ago

1.2.0

8 years ago

1.1.0

8 years ago

1.0.0

8 years ago