forked from hackclub/sprig
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
181 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { render, html } from "uhtml"; | ||
import { dispatch } from "../dispatch.js"; | ||
import { global_state } from "../global_state.js"; | ||
import { style } from "./style.js"; | ||
import { RGBA_to_hex, transparentBg } from "../palette.js"; | ||
|
||
export function createColorPicker(target) { | ||
const state = { color: '' }; | ||
|
||
const r = () => render(target, html` | ||
<style>${style}</style> | ||
<div class="color-picker-outer"> | ||
<h3>color picker:</h3> | ||
<div class="color-picker-inner"> | ||
${global_state.palette.map(drawColorButton)} | ||
</div> | ||
</div> | ||
`); | ||
|
||
const drawColorButton = (color) => { | ||
let style = `background-color: ${RGBA_to_hex(color[1])};`; | ||
color[1][3] === 0 ? style += `background-image: url("${transparentBg}");` : `` | ||
|
||
return html` | ||
<div | ||
class=${state.color === color[0] ? "active" : ""} | ||
style=${style} | ||
@click=${() => { | ||
state.color = color[0]; | ||
dispatch("SET_EDITOR_TEXT", { text: state.color, range: global_state.editRange }); | ||
r(); | ||
}}> | ||
</div> | ||
` | ||
}; | ||
|
||
r(); | ||
return { | ||
loadInitValue({ text }) { | ||
if (global_state.palette.find(([key]) => key === text)) state.color = text; | ||
r(); | ||
} | ||
}; | ||
} | ||
|
||
class ColorPicker extends HTMLElement { | ||
constructor() { | ||
super(); | ||
} | ||
|
||
connectedCallback() { | ||
const shadow = this.attachShadow({ mode: "open" }); | ||
const methods = createColorPicker(shadow); | ||
for (const i in methods) this[i] = methods[i]; | ||
} | ||
} | ||
|
||
customElements.define("color-picker", ColorPicker); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
export const style = ` | ||
.color-picker-outer { | ||
background: var(--bg-floating); | ||
height: 100%; | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
flex-direction: column; | ||
gap: 16px; | ||
} | ||
h3 { | ||
color: #ffffff; | ||
text-align: center; | ||
margin: 0; | ||
} | ||
.color-picker-inner { | ||
width: 380px; | ||
height: 300px; | ||
background: transparent; | ||
display: grid; | ||
grid-template-columns: repeat(4, 1fr); | ||
gap: 10px; | ||
padding: 10px; | ||
} | ||
.color-picker-inner div { | ||
border: 2px solid transparent; | ||
border-radius: 4px; | ||
cursor: pointer; | ||
} | ||
.color-picker-inner div.active { | ||
border: 2px solid var(--switch-fg-high); | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
|
||
export const style = ` | ||
:root { box-sizing: border-box; } | ||
* { box-sizing: inherit; } | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters