Skip to content

Commit

Permalink
feat(todomvc): add icons to example app
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzofox3 committed Feb 23, 2024
1 parent f3ffa32 commit be05ca1
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 6 deletions.
7 changes: 7 additions & 0 deletions apps/todomvc/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,19 @@ import {
} from './todo.service.ts';
import { todoListControlsView } from './views/todo-list-controls.view.js';
import { compose } from './utils.js';
import { UIIcon } from './components/ui-icon.component';

const connectWithView = compose([
connectTodoService,
withView<{ todoService: TodoService }, { state: TodoServiceState }>,
]);

define('ui-icon', UIIcon, {
observedAttributes: ['name'],
shadow: {
mode: 'open',
},
});
define('app-todo', withView(TodoItemView), {
shadow: { mode: 'open', delegatesFocus: true },
observedAttributes: ['completed'],
Expand Down
13 changes: 13 additions & 0 deletions apps/todomvc/components/icons.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
30 changes: 30 additions & 0 deletions apps/todomvc/components/ui-icon.component.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const template = document.createElement('template');
template.innerHTML = `
<style>
:host {
display: inline-block;
}
svg {
width: 100%;
height: 100%;
fill: currentColor;
}
</style>
<svg>
<use id="use" xlink:href=""></use>
</svg>
`;

const spriteURL = `/components/icons.svg`;

export const UIIcon = function* ({ $host, $root }) {
$root.replaceChildren(template.content.cloneNode(true));
$root.querySelector('svg').setAttribute('aria-hidden', 'true');
while (true) {
const iconName = $host.getAttribute('name');
$root
.getElementById('use')
.setAttribute('xlink:href', `${spriteURL}#${iconName}`);
yield;
}
};
2 changes: 1 addition & 1 deletion apps/todomvc/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ <h1>Todo list app</h1>
name="new-content"
type="text"
/>
<button>Add +</button>
<button><ui-icon name="plus-circle"></ui-icon>add</button>
</div>
</form>
</app-add-todo>
Expand Down
19 changes: 16 additions & 3 deletions apps/todomvc/theme.css
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,19 @@ h1 {
font-size: 1.5em;
}

ui-icon {
--_size: var(--size, 1em);
aspect-ratio: 1 / 1;
display: inline-block;
width: var(--_size);
height: var(--_size);
}

:is(span, button, a):has(ui-icon:first-child) {
display: flex;
align-items: center;
gap: 8px;
}

input[type=text],
button,
Expand All @@ -73,14 +86,15 @@ button,
--_color: var(--color, var(--font-color));
--_shadow-color: var(--shadow-color, lightgray);
--_bg-color: var(--bg-color, var(--surface-bg));
--_font-size: var(--font-size, 0.9em);

white-space: nowrap;
border: 1px solid var(--_color);
color: var(--_color);
border-radius: 4px;
box-shadow: 0 2px 2px 0 var(--_shadow-color);
background: var(--_bg-color);
font-size: 0.9em;
font-size: var(--_font-size);
padding: 0.3em;
}

Expand Down Expand Up @@ -171,12 +185,11 @@ app-todo {
grid-template-areas:
"filter filter filter"
"toggle-all info clear-all";
grid-template-columns: 100px 1fr 140px;
grid-template-columns: 100px 1fr 9em;
gap: 0.5em;
align-items: center;
font-size: 0.8em;


> fieldset {
grid-area: filter;
margin-inline: auto;
Expand Down
2 changes: 1 addition & 1 deletion apps/todomvc/views/add-todo.view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const AddTodoView: ViewFactory<{ todoService: TodoService }> = ({
name="new-content"
type="text"
/>
<button>Add +</button>
<button><ui-icon name="plus-circle"></ui-icon>add</button>
</div>
</form>`;
};
13 changes: 12 additions & 1 deletion apps/todomvc/views/todo-item.view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export const TodoItemView: ViewFactory = ({ html, $host }) => {
#remove-button {
--color: var(--danger-color, red);
display: flex;
align-items: center;
gap: 8px;
--font-size: 0.8em;
}
label {
Expand All @@ -30,6 +34,13 @@ export const TodoItemView: ViewFactory = ({ html, $host }) => {
align-items: center;
gap: var(--_gap);
}
ui-icon {
aspect-ratio: 1 / 1;
display: inline-block;
width: 1em;
height: 1em;
}
</style>
<label
><input
Expand All @@ -43,7 +54,7 @@ export const TodoItemView: ViewFactory = ({ html, $host }) => {
</span>
</label>
<button id="remove-button" part="button" @click="${handleClick}">
X<span>Remove</span>
<ui-icon name="x-circle"></ui-icon><span>remove</span>
</button>`;

function dispatch(eventName) {
Expand Down
1 change: 1 addition & 0 deletions apps/todomvc/views/todo-list-controls.view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export const todoListControlsView: ViewFactory<
id="clear-completed"
@click="${todoService.clearCompleted}"
>
<ui-icon name="recycle"></ui-icon>
Clear completed
</button>`
: null} `
Expand Down

0 comments on commit be05ca1

Please sign in to comment.