Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/components/pg/color/__examples__/basic/basic.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
<div class="example">
<pg-color part="color1" value="#000"></pg-color>
<code>Selected: <span part="colorSelected"></span></code>

<code>
Selected:
<span part="colorSelected">No color selected</span>
<button part="copyButton" disabled>Copy</button>
</code>
</div>
29 changes: 28 additions & 1 deletion src/components/pg/color/__examples__/basic/basic.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, Part, Prop } from '@pictogrammers/element';
import { Component, Part } from '@pictogrammers/element';
import PgColor from '../../color';

import template from './basic.html';
Expand All @@ -10,11 +10,38 @@ import template from './basic.html';
export default class XPgColorBasic extends HTMLElement {
@Part() $color1: PgColor;
@Part() $colorSelected: HTMLSpanElement;
@Part() $copyButton: HTMLButtonElement;

private selectedHex = '';

connectedCallback() {
this.$color1.addEventListener('select', (e: CustomEvent) => {
const { rgb, hex } = e.detail;

this.selectedHex = hex;
this.$colorSelected.innerText = `${hex} or rgb(${rgb.r}, ${rgb.g}, ${rgb.b})`;
this.$copyButton.disabled = false;
});

this.$copyButton.addEventListener('click', async () => {
try {
await navigator.clipboard.writeText(this.selectedHex);

const original = this.$copyButton.innerText;
this.$copyButton.innerText = 'Copied!';

setTimeout(() => {
this.$copyButton.innerText = original;
}, 1000);
} catch (error) {
console.error('Failed to copy color:', error);

this.$copyButton.innerText = 'Failed';

setTimeout(() => {
this.$copyButton.innerText = 'Copy';
}, 1000);
}
});
}
}