Code Monkey home page Code Monkey logo

custom-element's Introduction

custom-element

build crates.io docs.rs

Overview

What is a custom element / what is a web component?

First off "web components" are just another name for custom elements, and, in the words of Jake Lazaroff

They’re a set of W3C standards for building reusable HTML elements. You use them by writing a class for a custom element, registering a tag name and using it in your markup.

For us, this means that we can write HTML elements that run in the browser using Rust, and no other elements on the page have to know! It's a nice way to bridge the gap between HTML/JS/CSS and Rust/Wasm on the web. As a UI/library author, you can leverage all the flexibility of JS/HTML and still get all the tooling/performance/safety of Rust whenever and wherever you need it.

The Problem

Extending a JS class from Rust/Wasm is not currently supported by wasm-bindgen: rustwasm/wasm-bindgen#210, rustwasm/wasm-bindgen#210. This functionality, however is essential for creating a custom element on the web, since custom elements must inherit from HTMLElement (or some other valid subclass of HTMLElement).

The solution

This crates provide the JavaScript shim necessary for extending an arbitrary subclass of HTMLElement and forwards all custom element lifecycle method calls to the Rust struct you provide.

Why this crate rather than the other available ones?

  • Allows creating both autonomous custom elements AND customized built-in elements

  • Provides all the valid HtmlElement constructors for creating customized built-in elements--these are not provided by wasm-bindgen out-of-the-box, and it's not obvious how to access them otherwise.

  • Contains both simple examples for copying into your own project, and also advanced examples showing how to use this library to wrap Leptos components (for example) and render them from JavaScript/React

  • Less opinionated approach to creating custom elements. This library gives you the JavaScript constructor for the wrapper class around your struct, and you decide what to do with it.

Current Rough Edges

  • It's possible to run into the following issue if you are calling your custom element with both &mut and & pointers: rustwasm/wasm-bindgen#1578. The ways that I know of to get around this issue are by either by scheduling mutable calls to the custom element instance to happen once the current function scope ends or by adding some indirection through Rc<RefCell<T>> If you have other ideas of how to reduce this issue, I'd be interested in a message or a PR.

Other libraries you may be interested in

custom-element's People

Contributors

austintheriot avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.