Skip to content

Commit

Permalink
feat(icon): create an icon element
Browse files Browse the repository at this point in the history
refactor: use the icon element instead of just the icon function

fix(icon): remove faulty paths
  • Loading branch information
daenub authored May 23, 2024
1 parent 5c79621 commit 0a8351c
Show file tree
Hide file tree
Showing 21 changed files with 187 additions and 132 deletions.
14 changes: 9 additions & 5 deletions src/components/breadcrumb/Breadcrumb.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { createRef, ref } from "lit/directives/ref.js"
import { classMap } from "lit/directives/class-map.js"

import styles from "./breadcrumb.css"
import { Icon } from "../icon/icon.js"
import "../icon/leu-icon.js"
import "../menu/leu-menu.js"
import "../menu/leu-menu-item.js"
import "../popup/leu-popup.js"
Expand Down Expand Up @@ -225,7 +225,9 @@ export class LeuBreadcrumb extends LitElement {

return html`
<li class="breadcrumbs__item" data-dropdown-toggle>
<span class="breadcrumbs__icon">${Icon("angleRight")}</span>
<span class="breadcrumbs__icon"
><leu-icon name="angleRight"></leu-icon
></span>
<leu-popup
?active=${this._isDropdownOpen}
placement="bottom-start"
Expand Down Expand Up @@ -281,7 +283,9 @@ export class LeuBreadcrumb extends LitElement {
<ol class="breadcrumbs__list" ref=${ref(this._containerRef)}>
${showBackOnly
? html` <li class="breadcrumbs__item breadcrumbs__item--back">
<span class="breadcrumbs__icon">${Icon("arrowLeft")}</span>
<span class="breadcrumbs__icon"
><leu-icon name="arrowLeft"></leu-icon
></span>
<a class="breadcrumbs__link" href=${parentItem.href}
>${parentItem.label}</a
>
Expand All @@ -292,8 +296,8 @@ export class LeuBreadcrumb extends LitElement {
<li class="breadcrumbs__item">
${index > 0
? html`<span class="breadcrumbs__icon"
>${Icon("angleRight")}</span
>` // First list item doesn't have an arrow
><leu-icon name="angleRight"></leu-icon
></span>` // First list item doesn't have an arrow
: nothing}
${index === list.length - 1
? item.label // Last list item doesn't contain a link
Expand Down
8 changes: 4 additions & 4 deletions src/components/button/Button.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { html, nothing, LitElement } from "lit"
import { classMap } from "lit/directives/class-map.js"
import { ifDefined } from "lit/directives/if-defined.js"

import { Icon } from "../icon/icon.js"
import { HasSlotController } from "../../lib/hasSlotController.js"
import "../icon/leu-icon.js"

import styles from "./button.css"

Expand Down Expand Up @@ -131,7 +131,7 @@ export class LeuButton extends LitElement {
renderIconBefore() {
if (this.icon && this.iconPosition === "before") {
return html`<div class="icon-wrapper icon-wrapper--before">
${Icon(this.icon, this.getIconSize())}
<leu-icon name=${this.icon} size=${this.getIconSize()}></leu-icon>
</div>`
}

Expand All @@ -141,7 +141,7 @@ export class LeuButton extends LitElement {
renderIconAfter() {
if (this.icon && this.iconPosition === "after") {
return html`<div class="icon-wrapper icon-wrapper--after">
${Icon(this.icon, this.getIconSize())}
<leu-icon name=${this.icon} size=${this.getIconSize()}></leu-icon>
</div>`
}

Expand All @@ -151,7 +151,7 @@ export class LeuButton extends LitElement {
renderExpandingIcon() {
if (typeof this.expanded !== "undefined" && this.variant === "ghost") {
return html`<div class="icon-wrapper icon-wrapper--expanded">
${Icon("angleDropDown", 24)}
<leu-icon name="angleDropDown" size="24"></leu-icon>
</div>`
}

Expand Down
2 changes: 1 addition & 1 deletion src/components/button/button.css
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ button.ghost.inverted:disabled {
}

/* icon-wrapper */
.icon-wrapper svg {
.icon-wrapper leu-icon {
display: block;
}

Expand Down
4 changes: 2 additions & 2 deletions src/components/button/stories/button.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { html } from "lit"
import { ifDefined } from "lit/directives/if-defined.js"
import { classMap } from "lit/directives/class-map.js"
import "../leu-button.js"
import { ICON_NAMES } from "../../icon/icon.js"
import { paths as iconPaths } from "../../icon/paths.js"
import {
BUTTON_VARIANTS,
BUTTON_TYPES,
Expand Down Expand Up @@ -71,7 +71,7 @@ function Template(args = {}) {
export const Regular = Template.bind({})
Regular.argTypes = {
content: { type: "string" },
icon: { control: "select", options: ICON_NAMES },
icon: { control: "select", options: Object.keys(iconPaths) },
iconPosition: { control: "select", options: ["before", "after"] },
type: { control: "radio", options: BUTTON_TYPES },
size: { control: "radio", options: BUTTON_SIZES },
Expand Down
7 changes: 3 additions & 4 deletions src/components/checkbox/Checkbox.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { html, LitElement } from "lit"
import { Icon } from "../icon/icon.js"

import "../icon/leu-icon.js"

import styles from "./checkbox.css"

Expand Down Expand Up @@ -27,8 +28,6 @@ export class LeuCheckbox extends LitElement {
super()
this.checked = false
this.disabled = false

this.checkIcon = Icon("check")
}

handleChange(event) {
Expand Down Expand Up @@ -56,7 +55,7 @@ export class LeuCheckbox extends LitElement {
.value=${this.value}
/>
<label for=${this.identifier} class="label">${this.label}</label>
<div class="icon">${this.checkIcon}</div>
<leu-icon class="icon" name="check"></leu-icon>
`
}
}
11 changes: 2 additions & 9 deletions src/components/chip/ChipRemovable.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { html } from "lit"

import { LeuChipBase } from "./Chip.js"
import { Icon } from "../icon/icon.js"
import "../icon/leu-icon.js"

/**
* @slot - The content of the chip
Expand All @@ -13,13 +13,6 @@ export class LeuChipRemovable extends LeuChipBase {
...LeuChipBase.properties,
}

constructor() {
super()

/** @internal */
this._removeIcon = Icon("close", 16)
}

handleClick() {
const customEvent = new CustomEvent("leu:remove", {
bubble: true,
Expand All @@ -31,7 +24,7 @@ export class LeuChipRemovable extends LeuChipBase {
render() {
return html`<button @click=${(e) => this.handleClick(e)} class="button">
<span class="label">${this.label}</span>
<div class="icon">${this._removeIcon}</div>
<leu-icon name="close" size="16" class="icon"></leu-icon>
</button>`
}
}
4 changes: 0 additions & 4 deletions src/components/chip/chip.css
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,3 @@
text-overflow: ellipsis;
white-space: nowrap;
}

.icon svg {
display: block;
}
47 changes: 47 additions & 0 deletions src/components/icon/Icon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { html, svg, LitElement } from "lit"
import styles from "./icon.css"
import { paths } from "./paths.js"

/**
* A component to render all defined zhWeb icons.
* The `fill` of the icon is set to `currentColor` and
* can be overriden by setting the css `color` property.
*
* @tagname leu-icon
* @prop {import("./paths").IconPathName} name - The name of the icon to display.
* @prop {number} size - Width and height of the icon. A icon will always be displayed as a square.
*/
export class LeuIcon extends LitElement {
static styles = styles

static properties = {
name: { type: String, reflect: true },
size: { type: Number, reflect: true },
}

constructor() {
super()

/**
* @type {import("./paths").IconPathName}
*/
this.name = "addNew"
this.size = 24
}

render() {
const iconPath = paths[this.name]

return html`
<svg
width="${this.size}"
height="${this.size}"
fill="currentColor"
viewBox="0 0 24 24"
fill-rule="evenodd"
>
${svg`<path d=${iconPath} />`}
</svg>
`
}
}
3 changes: 3 additions & 0 deletions src/components/icon/icon.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
svg {
display: block;
}
6 changes: 6 additions & 0 deletions src/components/icon/leu-icon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { defineElement } from "../../lib/defineElement.js"
import { LeuIcon } from "./Icon.js"

export { LeuIcon }

defineElement("icon", LeuIcon)
41 changes: 4 additions & 37 deletions src/components/icon/icon.js → src/components/icon/paths.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions src/components/icon/stories/icon.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { html } from "lit"
import "../leu-icon.js"
import { ifDefined } from "lit/directives/if-defined.js"
import { paths } from "../paths.js"

export default {
title: "Icon",
component: "leu-icon",
argTypes: {
name: {
control: "select",
options: Object.keys(paths),
},
color: {
control: {
type: "color",
presetColors: ["#009ee0", "#d93c1a", "#1a7f1f"],
},
},
},
}

function Template({ name, size, color }) {
return html` <leu-icon
style="color: ${color}"
name=${ifDefined(name)}
size=${ifDefined(size)}
></leu-icon>`
}

export const Regular = Template.bind({})
Regular.args = {
size: 24,
}

export const Small = Template.bind({})
Small.args = {
size: 16,
}

export const Colored = Template.bind({})
Colored.args = {
name: "smileyDevastated",
size: 24,
color: "#d93c1a",
}
Loading

0 comments on commit 0a8351c

Please sign in to comment.