Skip to content

Commit

Permalink
Implemented token as web components
Browse files Browse the repository at this point in the history
  • Loading branch information
aschaeffer committed Apr 30, 2024
1 parent f6dddb7 commit 7e63132
Show file tree
Hide file tree
Showing 15 changed files with 261 additions and 44 deletions.
8 changes: 5 additions & 3 deletions components/01-atoms/tag/_preview.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@
<link rel="icon" type="image/png" href="{{ path '/favicon/favicon.png' }}">
<link rel="stylesheet" href="{{ path '/css/reactive-graph.css' }}">
<title>Preview</title>
<link rel="preload" href="{{ path '/reactive-graph-design_bg.wasm' }}" crossorigin="anonymous" as="fetch" type="application/wasm">
<link rel="preload" href="{{ path '/reactive-graph-design_bg.wasm' }}" crossorigin="anonymous" as="fetch"
type="application/wasm">
<link rel="modulepreload" href="{{ path '/reactive-graph-design.js' }}" crossorigin="anonymous">
</head>
<body>
<main>
<details open>
<summary>
<span class="font-black">&lt;p&gt;
{{#if _target.context.renderAsCss}}CSS Styled{{/if}}
{{#if _target.context.renderAsWebComponents}}Web Components{{/if}}
{{#if _target.context.renderAsCss}}CSS Styled{{/if}}
{{#if _target.context.renderAsWebComponents}}Web Components{{/if}}
</span>
</summary>
<p>This is a {{{ yield }}} tag</p>
Expand Down Expand Up @@ -90,6 +91,7 @@
</main>
<script type="module">
import init, * as bindings from "{{ path '/reactive-graph-design.js' }}";
init("{{ path '/reactive-graph-design_bg.wasm' }}");
window.wasmBindings = bindings;
</script>
Expand Down
107 changes: 91 additions & 16 deletions components/01-atoms/token/_preview.hbs
Original file line number Diff line number Diff line change
@@ -1,24 +1,99 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" type="image/svg+xml" href="{{ path '/favicon/favicon.svg' }}">
<link rel="icon" type="image/png" href="{{ path '/favicon/favicon.png' }}">
<link rel="stylesheet" href="{{ path '/css/reactive-graph.css' }}">
<title>Preview</title>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" type="image/svg+xml" href="{{ path '/favicon/favicon.svg' }}">
<link rel="icon" type="image/png" href="{{ path '/favicon/favicon.png' }}">
<link rel="stylesheet" href="{{ path '/css/reactive-graph.css' }}">
<title>Preview</title>
<link rel="preload" href="{{ path '/reactive-graph-design_bg.wasm' }}" crossorigin="anonymous" as="fetch"
type="application/wasm">
<link rel="modulepreload" href="{{ path '/reactive-graph-design.js' }}" crossorigin="anonymous">
</head>
<body>
<main>
<main>
<details open>
<summary>
<span class="font-black">&lt;p&gt;
{{#if _target.context.renderAsCss}}CSS Styled{{/if}}
{{#if _target.context.renderAsWebComponents}}Web Components{{/if}}
</span>
</summary>
<p>This is a {{{ yield }}} token</p>
</details>
<details open>
<summary>
<span class="font-black">&lt;h6&gt;
{{#if _target.context.renderAsCss}}CSS Styled{{/if}}
{{#if _target.context.renderAsWebComponents}}Web Components{{/if}}
</span>
</summary>
<h6>This is a {{{ yield }}} token</h6>
</details>
<details open>
<summary>
<span class="font-black">&lt;h5&gt;
{{#if _target.context.renderAsCss}}CSS Styled{{/if}}
{{#if _target.context.renderAsWebComponents}}Web Components{{/if}}
</span>
</summary>
<h5>This is a {{{ yield }}} token</h5>
</details>
<details open>
<summary>
<span class="font-black">&lt;h4&gt;
{{#if _target.context.renderAsCss}}CSS Styled{{/if}}
{{#if _target.context.renderAsWebComponents}}Web Components{{/if}}
</span>
</summary>
<h4>This is a {{{ yield }}} token</h4>
</details>
<details open>
<summary>
<span class="font-black">&lt;h3&gt;
{{#if _target.context.renderAsCss}}CSS Styled{{/if}}
{{#if _target.context.renderAsWebComponents}}Web Components{{/if}}
</span>
</summary>
<h3>This is a {{{ yield }}} token</h3>
</details>
<details open>
<summary>
<span class="font-black">&lt;h2&gt;
{{#if _target.context.renderAsCss}}CSS Styled{{/if}}
{{#if _target.context.renderAsWebComponents}}Web Components{{/if}}
</span>
</summary>
<h2>This is a {{{ yield }}} token</h2>
</details>
<details open>
<summary>
<span class="font-black">&lt;h1&gt;
{{#if _target.context.renderAsCss}}CSS Styled{{/if}}
{{#if _target.context.renderAsWebComponents}}Web Components{{/if}}
</span>
</summary>
<h1>This is a {{{ yield }}} token</h1>
</details>
<details open>
<summary>
<span class="font-black">&lt;blockquote&gt; &lt;p&gt;
{{#if _target.context.renderAsCss}}CSS Styled{{/if}}
{{#if _target.context.renderAsWebComponents}}Web Components{{/if}}
</span>
</summary>
<blockquote>
<p>This is a {{{ yield }}} token</p>
<h6>This is a {{{ yield }}} token</h6>
<h5>This is a {{{ yield }}} token</h5>
<h4>This is a {{{ yield }}} token</h4>
<h3>This is a {{{ yield }}} token</h3>
<h2>This is a {{{ yield }}} token</h2>
<h1>This is a {{{ yield }}} token</h1>
<blockquote><p>This is a {{{ yield }}} token</p></blockquote>
</main>
</blockquote>
</details>
</main>
<script type="module">
import init, * as bindings from "{{ path '/reactive-graph-design.js' }}";
init("{{ path '/reactive-graph-design_bg.wasm' }}");
window.wasmBindings = bindings;
</script>
</body>
</html>
1 change: 0 additions & 1 deletion components/01-atoms/token/token--component.hbs

This file was deleted.

1 change: 0 additions & 1 deletion components/01-atoms/token/token--entity-instance.hbs

This file was deleted.

1 change: 0 additions & 1 deletion components/01-atoms/token/token--entity-type.hbs

This file was deleted.

1 change: 0 additions & 1 deletion components/01-atoms/token/token--punctuation.hbs

This file was deleted.

1 change: 0 additions & 1 deletion components/01-atoms/token/token--relation-instance.hbs

This file was deleted.

1 change: 0 additions & 1 deletion components/01-atoms/token/token--relation-type.hbs

This file was deleted.

80 changes: 66 additions & 14 deletions components/01-atoms/token/token.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,99 @@
{
"name": "default",
"context": {
"token": "string"
"ty": "string"
}
},
{
"name": "punctuation",
"name": "component__css",
"context": {
"token": "punctuation"
"ty": "component",
"text": "Component",
"renderAsCss": true
}
},
{
"name": "component",
"name": "component__web-components",
"context": {
"token": "component"
"ty": "component",
"text": "Component",
"renderAsCss": false,
"renderAsWebComponents": true
}
},
{
"name": "entity-type",
"name": "entity-type__css",
"context": {
"token": "entity type"
"ty": "entity-type",
"text": "Entity Type",
"renderAsCss": true
}
},
{
"name": "entity-instance",
"name": "entity-type__web-components",
"context": {
"token": "entity instance"
"ty": "entity-type",
"text": "Entity Type",
"renderAsCss": false,
"renderAsWebComponents": true
}
},
{
"name": "relation-type",
"name": "entity-instance__css",
"context": {
"token": "relation type"
"ty": "entity-instance",
"text": "Entity Instance",
"renderAsCss": true
}
},
{
"name": "relation-instance",
"name": "entity-instance__web-components",
"context": {
"token": "relation instance"
"ty": "entity-instance",
"text": "Entity Instance",
"renderAsCss": false,
"renderAsWebComponents": true
}
},
{
"name": "relation-type__css",
"context": {
"ty": "relation-type",
"text": "Relation Type",
"renderAsCss": true
}
},
{
"name": "relation-type__web-components",
"context": {
"ty": "relation-type",
"text": "Relation Type",
"renderAsCss": false,
"renderAsWebComponents": true
}
},
{
"name": "relation-instance__css",
"context": {
"ty": "relation-instance",
"text": "Relation Instance",
"renderAsCss": true
}
},
{
"name": "relation-instance__web-components",
"context": {
"ty": "relation-instance",
"text": "Relation Instance",
"renderAsCss": false,
"renderAsWebComponents": true
}
}
],
"context": {
"token": "default"
"ty": "string",
"text": "string",
"renderAsCss": true,
"renderAsWebComponents": false
}
}
7 changes: 6 additions & 1 deletion components/01-atoms/token/token.hbs
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
<span class="token string">{{token}}</span>
{{#if renderAsCss}}
<span class="token rg-{{ty}}">{{text}}</span>
{{/if}}
{{#if renderAsWebComponents}}
<rg-token ty="{{ty}}">{{text}}</rg-token>
{{/if}}
9 changes: 9 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ <h1>Reactive Graph - Design - Web Components</h1>
<rg-tag ty="relation-instance">Relation Instance</rg-tag>
<br/>
</details>
<details open>
<summary>Tokens</summary>
<rg-token ty="component">Component</rg-token>
<rg-token ty="entity-type">Entity Type</rg-token>
<rg-token ty="relation-type">Relation Type</rg-token>
<rg-token ty="entity-instance">Entity Instance</rg-token>
<rg-token ty="relation-instance">Relation Instance</rg-token>
<br/>
</details>
</main>
</body>
</html>
10 changes: 10 additions & 0 deletions src/atoms/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,11 @@
use crate::atoms::tag::RgTag;
use crate::atoms::token::RgToken;
use crate::base::CustomElementCreator;

pub mod tag;
pub mod token;

pub(crate) fn define_elements() {
RgTag::define_element();
RgToken::define_element();
}
File renamed without changes.
73 changes: 73 additions & 0 deletions src/atoms/token.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use rs_web_component::Component;
use wasm_bindgen::prelude::*;
use web_sys::HtmlElement;
use web_sys::ShadowRoot;
use web_sys::ShadowRootInit;
use web_sys::ShadowRootMode;

use crate::base::CustomElementCreator;
use crate::base::RootVal;
use crate::base::ThisVal;

pub struct RgToken {
root: RootVal,
this: ThisVal,
}

impl CustomElementCreator for RgToken {
fn tag_name() -> String {
"rg-token".to_string()
}

fn create_component() -> Box<dyn Component> {
Box::new(Self {
root: RootVal::None,
this: ThisVal::None,
})
}
}

impl Component for RgToken {
fn init(&mut self, this: HtmlElement) {
self.this = ThisVal::Value(this);
}

fn observed_attributes(&self) -> Vec<String> {
return vec!["ty".to_string()];
}

fn attribute_changed_callback(&self, _name: String, _old_value: JsValue, _new_value: JsValue) {}

fn connected_callback(&mut self) {
self.root = RootVal::Value(self.get_this().attach_shadow(&ShadowRootInit::new(ShadowRootMode::Open)).unwrap());
self.rerender();
}

fn disconnected_callback(&self) {}
}

impl RgToken {
fn render(&self) -> String {
let ty = self.get_this().get_attribute("ty").unwrap_or(String::new());
let text = self.get_this().inner_html();
format!("<link rel=\"stylesheet\" href=\"/css/reactive-graph.css\"><span class=\"token rg-{ty}\">{text}</span>",).to_string()
}

fn rerender(&self) {
self.get_root().set_inner_html(self.render().as_str());
}

fn get_root(&self) -> &ShadowRoot {
return match &self.root {
RootVal::Value(root) => &root,
RootVal::None => panic!("not a root!"),
};
}

fn get_this(&self) -> &HtmlElement {
match &self.this {
ThisVal::Value(val) => val,
ThisVal::None => panic!("not an HtmlElement"),
}
}
}
5 changes: 1 addition & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use wasm_bindgen::prelude::wasm_bindgen;

use crate::atoms::tag::RgTag;
use crate::base::CustomElementCreator;

pub mod atoms;
pub mod molecules;
pub mod organisms;
Expand All @@ -11,5 +8,5 @@ pub mod base;

#[wasm_bindgen(start)]
fn run() {
RgTag::define_element();
atoms::define_elements();
}

0 comments on commit 7e63132

Please sign in to comment.