i want to add color picker in widget
https://magento.stackexchange.com/a/190260/73525
This above link is working for system config
same thing i want in widget as field
Any one have idea about this
i want to add color picker in widget
https://magento.stackexchange.com/a/190260/73525
This above link is working for system config
same thing i want in widget as field
Any one have idea about this
Here's a simple version.
<?php
namespace DnTukadiya\HyvaWidgets\Block\Widget\Field;
use Magento\Framework\View\Element\Template;
use Magento\Widget\Block\BlockInterface;
class Color extends Template implements BlockInterface
{
public function prepareElementHtml(\Magento\Framework\Data\Form\Element\AbstractElement $element)
{
$defaultColor = "#D1D5DB";
$value = $element->getValue() ?? $defaultColor;
$element->setData('after_element_html', "
<div style='display:flex;flex-direction:row;gap:10px;justify-content:start;align-items:center;'>
<input type='text' value='{$value}' id='{$element->getHtmlId()}' name='{$element->getName()}' oninput='document.querySelector(\"#{$element->getHtmlId()}-color-picker\").value = this.value'>
<input type='color' id='{$element->getHtmlId()}-color-picker' oninput='document.querySelector(\"#{$element->getHtmlId()}\").value = this.value'>
</div>
");
$element->setValue(null);
return $element;
}
}
Try this:
<parameter name="color" xsi:type="block" required="false" visible="true">
<label translate="true">Color</label>
<block class="Vendor\Module\Block\Widget\Color" />
</parameter>
<?php
namespace Vendor\Module\Block\Widget;
use Magento\Framework\View\Element\Template;
use Magento\Widget\Block\BlockInterface;
class Color extends Template implements BlockInterface
{
public function prepareElementHtml(\Magento\Framework\Data\Form\Element\AbstractElement $element)
{
$defaultColor = "#8ec63f";
$value = $element->getValue() ?: defaultColor;
$element->setData('after_element_html', '
<input type="text"
style="height: 55px; width: 55px;"
value="' . $value . '"
id="' . $element->getHtmlId() . '"
name="' . $element->getName() . '"
>
<script type="text/javascript">
require(["jquery", "jquery/colorpicker/js/colorpicker"], function ($) {
$currentElement = $("#' . $element->getHtmlId() . '");
$currentElement.css("backgroundColor", "'. $value .'");
$currentElement.ColorPicker({
color: "' . $value . '",
onChange: function (hsb, hex, rgb) {
$currentElement.css("backgroundColor", "#" + hex).val("#" + hex);
}
});
});
</script><style>.colorpicker {z-index: 10010}</style>');
$element->setValue(null);
return $element;
}
}
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<head>
<css src="jquery/colorpicker/css/colorpicker.css"/>
</head>
</page>
Do you check official documentation? https://devdocs.magento.com/guides/v2.4/ui_comp_guide/components/ui-colorpicker.html
we need to add this code for the color picker
<div>
<input type="color" id="head" name="head" value="#46E265" />
<label for="head">choose color</label>
</div>