Dumbass – UI Made Simple

栏目: IT技术 · 发布时间: 4年前

内容简介:Make components from cross-browser web standards without thinking too hard.Kernighan's Law is named forWhile hyperbolic, Kernighan's Law makes the argument that simple code is to be preferred over complex code, because debugging any issues that arise in co

:bug: dumbass

Chosen by toddlers, insects, and stupid coders.

Be boring, be dumb. Be too stupid for complex tools.

Make components from cross-browser web standards without thinking too hard.

Why?

Kernighan's Law

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
(Brian Kernighan)

Kernighan's Law is named for Brian Kernighan and derived from a quote from Kernighan and Plauger's book The Elements of Programming Style :

Everyone knows that debugging is twice as hard as writing a program in the first place. So if you're as clever as you can be when you write it, how will you ever debug it?

While hyperbolic, Kernighan's Law makes the argument that simple code is to be preferred over complex code, because debugging any issues that arise in complex code may be costly or even infeasible.

So, what's this?

No JSX, no Shadow DOM, no fancy framworks, no opinions.

  • Just HTML, CSS and JavaScript —No JSX, no Shadow DOM, no fancy frameworks, no opinions.
  • Stop learning, stagnate! —Use the syntax you already know. Stop learning new things. Do more with what's already here.
  • Crazy and fun, but in a serious way —Dumbass is the tool for people who don't want to think too hard to make UI.

To learn more ...oh wait, you already know enough.

Gorgeous dumbass

function Spin(n) {
  return d`  
    <div 
      wheel:passive=${spin}
      touchmove:passive=${move}
    >
      <h1>
        <progress 
          max=1000
          value=${n}
        ></progress>
        <hr>
        <input 
          input=${step}
          type=number 
          value=${n}>
    </div>
  `;
}

Still not bored?

You soon will be. Nothing amazing here: Play with the full example on CodePen

See even more boring code in a 250 line TodoMVC test

Install mantras

Install dumbass with npm:

npm i --save dumbass

Parcel or Webpack dumbass and import:

import { d } from "dumbass"

See a CodeSandbox how-to of above

Or import in a module:

<script type="module">
  import { d } from "https://unpkg.com/dumbass"
</script>

See a CodePen how-to of above

Basic Examples

Components

Components are pure views. Functions that take something and return dumbass objects . If the view needs state, it can take it as a parameter.

Defining

const Component = state => d`<h1>${state}</h1>`

A dumbass object is your HTML template. It's just a standard JavaScript template literal you tag with the d function.

Nesting

Nest components by slotting the result of calling the component into the template of the parent component.

const Parent = state => d`<main>${Title(state)}</main>`;

Mounting

The first time you call a top-level component, you should mount it to the document.

Parent("Hello").to('body', 'beforeEnd');

Updating

In order to update some view, just call that function again. It's as simple as that.

Parent("Greetings");
setTimeout(() => Parent("Hi"), 3000);

Real-world example

In the example below the App component nests TodoList and Footer components.

It also calls the newTodoIfEnter updator function on the keydown event of that input tag.

function App(state) {
  const {list} = state;
  return d`
    <header>
      <h1>todos</h1>
      <input placeholder="What needs to be done?" autofocus
        keydown=${newTodoIfEnter} 
      >
    </header>
    <main>
      ${TodoList(list)}
      ${Footer()}
    </main>
  `;
}

function TodoList(list) {
  return d`
    <ul class=todo-list>
      ${list.map(Todo)}
    </ul>
  `;
}

You'll notice we don't pass state (the whole client state object) to every component. Components can define the parameters they take, and it's up to their embedding components to pass them the correct state.

You might also notice we're passing an Array into the template (in TodoList ). That's OK so long as the Array only contains dumbass objects.

Updating on events

In order to update state and view in response to user input, you define updator functions and pass them into a template slot as the value of an attribute named for the event you respond to. The above real world example showed the newTodoIfEnter updator was hooked to occur on the keydown event.

function newTodoIfEnter(keyEvent) {
    if ( keyEvent.key !== 'Enter' ) return;
    
    State.todos.push(makeTodo(keyEvent.target.value));
    TodoList(State.todos);
    keyEvent.target.value = '';
  }

Updator functions noramlly do two things:

  1. Update some state.
  2. Call the components that change in response to that update.

They're a loose convention that makes explicit the mapping between state changes and view functions / components.

You might also notice we manually empty the value. There's no strict binding between input values and state as enforced in more opinionated frameworks. You're free to write that sort of thing if you like, e.g: const NumberInput = n => d`<input type=number value=${n}>` ;

Properties

There are no such things as properties in this "framework". A Component is simply a JavaScript function that returns dumbass object, which is just a template literal tagged with the d function.

The way that state is passed through to sub-components is simply through function calls, and the way that attributes and content are applied to elements is simply by slots in the dumbass template, just like in the above examples.

Global State

Again, there is nothing special about "state" in this "framework". State is simply regular JavaScript variables. You use state the same way you use variables in your program. State needs to be in scope where you reference it.

State flows through the component tree via normal JavaScript function calls. A sub component receives state passed down from its parent component by a function call.

Routing

Routing is not natively provided so you need to wire it yourself. Here's an exmaple of one way to do it:

function changeHash(e) {
  e.preventDefault();
  history.replaceState(null,null,e.target.href);
  routeHash();
}

function routeHash() {
  switch(location.hash) {
    case "#/active":                listActive(); break;
    case "#/completed":             listCompleted(); break;
    case "#/":          default:    listAll(); break;
  }
}

And you'll also need a place to list the routes:

function Routes() {
  return d`
    <ul>
      <li>
        <a href="#/" click=${changeHash}
         #/" ? 'selected' : ''}">All</a>
      </li>
      <li>
        <a href="#/active" click=${changeHash}
         #/active" ? 'selected' : ''}">Active</a>
      </li>
      <li>
        <a href="#/completed" click=${changeHash}
         #/completed" ? 'selected' : ''}">Completed</a>
      </li>
    </ul>
  `
}

Most of the examples above are taken from in a 250 line TodoMVC test , the full code of which you can see here. .

Go forth, stagnate and be dumb!


以上所述就是小编给大家介绍的《Dumbass – UI Made Simple》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

C语言的科学和艺术

C语言的科学和艺术

罗伯茨 / 翁惠玉 / 机械工业出版社 / 2005-3 / 55.00元

《C语言的科学和艺术》是计算机科学的经典教材,介绍了计算机科学的基础知识和程序设计的专门知识。《C语言的科学和艺术》以介绍ANSI C为主线,不仅涵盖C语言的基本知识,而且介绍了软件工程技术以及如何应用良好的程序设计风格进行开发等内容。《C语言的科学和艺术》采用了库函数的方法,强调抽象的原则,详细阐述了库和模块化开发。此外,《C语言的科学和艺术》还利用大量实例讲述解决问题的全过程,对开发过程中常见......一起来看看 《C语言的科学和艺术》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具