Created
October 2, 2021 06:08
-
-
Save robertdrakedennis/35461b629722c2adbbe0c1938c4178d8 to your computer and use it in GitHub Desktop.
Laravel color picker using vanilla-colorful and skypack
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
@props([ | |
'id' => null, | |
'value' => null, | |
'text' => null, | |
'name' => null, | |
'default' => null, | |
]) | |
<div | |
{{ $attributes->merge(['title' => $value]) }} | |
> | |
<h4 id="{{ $id }}-text" style="color: {{ $default }}"> | |
@if($text) | |
{{ $text }} | |
@endif | |
</h4> | |
<hex-color-picker @if(!empty($value))color="{{ $value }}"@elseif(!empty($default))color="{{ $default }}"@endif></hex-color-picker> | |
<input | |
id="{{ $id }}-input" | |
name="{{ $name }}" | |
type="hidden" | |
@if(!empty($value))value="{{ $value }}"@elseif(!empty($default))value="{{ $default }}"@endif | |
/> | |
</div> | |
@push('scripts') | |
@once | |
<script type="module"> | |
import 'https://cdn.skypack.dev/vanilla-colorful'; | |
const picker = document.querySelector('hex-color-picker'); | |
picker.addEventListener('color-changed', (event) => { | |
let input = document.getElementById('{{ $id }}-input') | |
let preview = document.getElementById('{{ $id }}-text') | |
const newColor = event.detail.value; | |
preview.style.color = newColor; | |
input.setAttribute('value', newColor); | |
input.dispatchEvent(new Event('input', {bubbles: true})) | |
}); | |
</script> | |
@endonce | |
@endpush |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment