mirror of
https://github.com/tabler/tabler.git
synced 2026-08-29 13:21:29 +04:00
60 lines
1.6 KiB
Plaintext
60 lines
1.6 KiB
Plaintext
---
|
|
// The Coloris init <script> is captured via {% capture_script %} in Liquid, so it
|
|
// is registered with addPageScript synchronously here. We mirror the development
|
|
// build, which wraps the instance in window.tabler_colorpicker.
|
|
import { addPageScript } from '@shared/lib/page-scripts';
|
|
import InlineScript from '@shared/components/InlineScript.astro';
|
|
import site from '@data/site.json';
|
|
|
|
interface Props {
|
|
value?: string;
|
|
id?: string | number;
|
|
alpha?: boolean;
|
|
format?: string | false;
|
|
/** swatches-only param in ui/colorpicker.html */
|
|
swatchesOnly?: boolean;
|
|
class?: string;
|
|
}
|
|
|
|
const {
|
|
value,
|
|
id = 'default',
|
|
alpha = false,
|
|
format = false,
|
|
swatchesOnly,
|
|
class: className,
|
|
} = Astro.props;
|
|
|
|
const colors = site.colors as Record<string, { prop: string }>;
|
|
|
|
const swatches = Object.values(colors)
|
|
.map((color) => `\n\t\t\t\twindow.getComputedStyle(document.body).getPropertyValue('${color.prop}'),`)
|
|
.join('');
|
|
|
|
const script = `<script>
|
|
document.addEventListener("DOMContentLoaded", function () {
|
|
window.tabler_colorpicker = window.tabler_colorpicker || {};
|
|
|
|
window.Coloris && (window.tabler_colorpicker["colorpicker-${id}"] = Coloris({
|
|
el: "#colorpicker-${id}",
|
|
selectInput: false,
|
|
alpha: ${alpha ? 'true' : 'false'},
|
|
${format ? `format: "${format}",` : ''}
|
|
${swatchesOnly ? 'swatchesOnly: true,' : ''}
|
|
swatches: [${swatches}
|
|
],
|
|
}))
|
|
})
|
|
</script>`;
|
|
|
|
addPageScript(script);
|
|
---
|
|
|
|
<input
|
|
type="text"
|
|
class={`form-control d-block${className ? ` ${className}` : ''}`}
|
|
id={`colorpicker-${id}`}
|
|
value={value}
|
|
/>
|
|
<InlineScript code={script} />
|