MCPcopy
hub / github.com/anthonyshort/deku

github.com/anthonyshort/deku @1.0.0 sqlite

repository ↗ · DeepWiki ↗ · release 1.0.0 ↗
189 symbols 344 edges 24 files 77 documented · 41%
README

Deku

version Circle CI js-standard-style Chat

A library for creating UI components using virtual DOM as an alternative to React. Deku has a smaller footprint (~6kb), a functional API, and doesn't support legacy browsers.

npm install deku virtual-element

You can also use Duo, Bower or download the files manually.

Example

import element from 'virtual-element'
import {render,tree} from 'deku'
import MyButton from './button'

var app = tree(



    <MyButton>Hello World!</MyButton>



)

render(app, document.body)

Introduction

Deku is a DOM renderer for virtual elements that also allows us to define custom element types. It runs diffing algorithm on these virtual elements to update the real DOM in a performant way.

Heads up: These examples are written using ES2015 syntax. You'll want to make sure you're familiar with modules and destructuring to follow along.

Virtual elements are plain objects that represent real DOM elements:

{
  type: 'button',
  attributes: { class: 'Button' },
  children: props.children
}

Which can then be rendered by Deku to the DOM. This example will render a button to the document.body with a class of Button:

import {render,tree} from 'deku'

var button = {
  type: 'button',
  attributes: { class: 'Button' }
}

// Create an app
var app = tree(button)

// Automatically re-renders the app when state changes
render(app, document.body)

You can define your own custom elements that can contain their own state. These are called components. Components are objects that (at the very least) have a render function. This render function is passed in a component object with these properties:

  • props: This is any external data
  • state: This is any internal data that is hidden from the world
  • id: The instance id of the component

Here's an example App component that renders a paragraph:

import {render,tree} from 'deku'

// Define our custom element. The render method should
// return a new virtual element.
var App = {
  render: function ({ props, state }) {
    return {
      type: 'p',
      attributes: { color: props.color }
    }
  }
}

// Then create a virtual element with our custom type
var app = tree({
  type: App, // <- custom type instead of a string
  attributes: { color: 'red' } // <- these become 'props'
})

// And render it to the DOM
render(app, document.body)

Virtual Elements

But these virtual elements aren't very easy to read. The good news is that you can use other libraries to add a DSL for creating these objects:

So you can use the virtual-element module to easily create these objects instead:

element('div', { class: "App" }, [
  element('button', { class: "Button" }, 'Click Me!')
])

And if you're using virtual-element you can also use JSX to make rendering nodes more developer friendly. This is equivalant to the previous example:




  <button class="Button">Click Me!</button>



JSX might seem offensive at first, but if you're already using Babel you get JSX for free. Think of it as a more familiar way to define tree structures. The rest of the examples will assume we're using JSX. You can go ahead and imagine the same syntax using the virtual-element DSL or the raw object format.

So the previous app example would look like this using JSX (notice that we're importing the virtual-element module this time):

import element from 'virtual-element'
import {render,tree} from 'deku'

// Define our custom element
var App = {
  render: function ({ props }) {
    return 

Hello World


  }
}

var app = tree(<App color="red" />)

// And render it to the DOM
render(app, document.body)

Custom Elements

So now we can start defining components in their own module and export them. Let's create a custom button element:

// button.js
import element from 'virtual-element'

let MyButton = {
  render ({props}) {
    return <button class="Button">{props.children}</button>
  }  
}

export {MyButton}

Then we can import it and render it in the same way:

// app.js
import element from 'virtual-element'
import {MyButton} from './button'
import {render,tree} from 'deku'

// We're using our custom MyButton element
var app = tree(



    <MyButton>Hello World!</MyButton>



)

render(app, document.body)

You can also render these same elements and custom elements on the server using renderString instead of render:

// server.js
import element from 'virtual-element'
import {MyButton} from './button'
import {renderString,tree} from 'deku'

let html = renderString(tree(



    <MyButton>Hello World!</MyButton>



))

That's all there is to it. Components can also have hook functions so you can do some work when they are created, removed or updated, and you can add state to your components.

Next steps

Tests

Deku is built with Browserify. You can run the tests in a browser by running make test. Learn how to build and work on Deku in the documentation.

Sauce Test Status

License

The MIT License (MIT) Copyright (c) 2015 Anthony Short

Extension points exported contracts — how you extend this code

PropLike (Interface)
* Prop types are constrained and informed by three interfaces: HasChildren, * Evented, and Keyed. * * In prac
docs/deku.d.ts
Application (Interface)
* An Application is a virtual tree extended with environment and plugin * information. In this form it is ready to mo
docs/deku.d.ts
Component (Interface)
* A "live" component providing access to the current properties and state * at a current time of a component managing
docs/deku.d.ts
PropSpec (Interface)
* Keys within a component's Property set may be configured and * deliminated by property specifications.
docs/deku.d.ts
Spec (Interface)
* A descriptor of Component creation, behavior, and lifecycle. * * Deku's primary interface is operated via speci
docs/deku.d.ts

Core symbols most depended-on inside this repo

end
called by 33
docs/examples/events/index.js
setup
called by 32
test/dom/index.js
teardown
called by 31
test/dom/index.js
trigger
called by 21
lib/render.js
setState
called by 15
lib/render.js
set
called by 15
docs/deku.d.ts
mount
called by 11
docs/deku.d.ts
toNative
called by 6
lib/render.js

Shape

Function 143
Method 21
Class 16
Interface 9

Languages

TypeScript100%

Modules by API surface

lib/render.js58 symbols
docs/examples/events/index.js29 symbols
test/dom/index.js26 symbols
docs/deku.d.ts21 symbols
docs/examples/iteration/react.js13 symbols
docs/examples/counter/react.js8 symbols
docs/examples/iteration/index.js6 symbols
docs/examples/counter/index.js6 symbols
lib/stringify.js5 symbols
docs/examples/like-button/react.js5 symbols
docs/examples/like-button/index.js3 symbols
test/string/index.js2 symbols

Dependencies from manifests, versioned

babelify6.4.0 · 1×
bfc0.3.1 · 1×
browserify12.0.1 · 1×
bump0.2.5 · 1×
component-emitter1.2.0 · 1×
component-raf1.2.0 · 1×
component-type1.2.0 · 1×
duo-babel6.0.0 · 1×
duo-serve0.14.5 · 1×
envify3.4.0 · 1×
express4.13.3 · 1×
fast.js0.1.1 · 1×

For agents

$ claude mcp add deku \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact