Jump to content

RockColorPicker - Super simple dependency-free color palette picker


bernhard
 Share

Recommended Posts

Happy new year everyone! New year, new module 😎

RockColorPicker

Super simple color picker for ProcessWire:

68747470733a2f2f692e696d6775722e636f6d2f3730314a6a39432e706e67

On non-required fields the selected color can be unselected by clicking the color again. The module also supports selecting colors via keyboard input (tab to focus color and then space/enter to select/unselect).

Setup

The colors of your field must be defined in /site/init.php:

$wire->addHookBefore(
  "RockColorPicker::colors",
  function (HookEvent $event) {
    // set colors of field "site_bgcolor"
    // labels are translatable
    $event->object->setColors('site_bgcolor', [
      'blue' => ['#acefee', __('blue')],
      'yellow' => ['#ffe36c', __('yellow')],
      'rose' => ['#ffc5df', __('rose')],
      'green' => ['#b5ffb2', __('green')],
      'violet' => ['#d5bfff', __('violet')],
    ]);

    // set colors of field "another_field"
    $event->object->setColors('another_field', [
      'red' => ['#ff0000', 'This is red'],
      'green' => ['#00ff00', 'This is green'],
      'blue' => ['#0000ff', 'This is blue'],
    ]);
  }
);

Accessing the field values

Formatted value:

68747470733a2f2f692e696d6775722e636f6d2f734f42633138682e706e67

Unformatted value:

68747470733a2f2f692e696d6775722e636f6d2f53746e4462576d2e706e67

Download

  • Like 5
  • Thanks 2
Link to comment
Share on other sites

So... the main difference to already existing color pickers here is the ability to give a preset of available colors?

That's nice. Feels more natural to select a visible color than just something with the label "blue" from a dropdown.

Next version is RockColorPickerCombo in which we can define foreground and background color combos? 😉

  • Like 1
Link to comment
Share on other sites

2 hours ago, wbmnfktr said:

So... the main difference to already existing color pickers here is the ability to give a preset of available colors?

Yes, and to define them in code (which is a lot easier for automated deployments) and to get rid of any external JS library just for selecting a single color.

2 hours ago, wbmnfktr said:

That's nice. Feels more natural to select a visible color than just something with the label "blue" from a dropdown.

That's exactly the use that I built it for 🙂 

Before:

XhreVFg.png

After:

4GuvKzT.png

2 hours ago, wbmnfktr said:

Next version is RockColorPickerCombo in which we can define foreground and background color combos? 😉

Not sure if there is any benefit here compared to creating two fields? What would be the exact use case?

  • Like 1
Link to comment
Share on other sites

5 minutes ago, bernhard said:

Before:

XhreVFg.png

Yeah... that's what I meant. It works, but it's not nice at all.

 

5 minutes ago, bernhard said:

Not sure if there is any benefit here compared to creating two fields? What would be the exact use case?

Well I was just kidding with that thought but... the benefit of a ColorPickerCombo would be that client's can't display yellow text on blue backgrounds just because they have seen it in PowerPoint. Instead they could only choose from white/blue, white/orange, red/black or any other combo from the presets. You could even use that for simple gradients of some kind. But don't take that too serious.

Link to comment
Share on other sites

Damn, I thought about gradient backgrounds the other day but totally forgot that today 😄 

I've implemented the color blocks with SVG so I can't simply support gradients at the moment, but using DIV instead it will work:

M4AMzPD.png

I'll refactor and add that tomorrow the other day! Then we have another distinction to other colorpickers 😉 😄 

  • Like 4
Link to comment
Share on other sites

Just pushed v1.1.0 - actually it is not a "color" picker any more 😅

68747470733a2f2f692e696d6775722e636f6d2f

    // custom CSS + HTML example
    $event->object->setColors('site_bgcolor', [
      // custom css
      'custom' => [
        'background: rgb(34,0,255); background: linear-gradient(90deg, rgba(34,0,255,1) 0%, rgba(0,0,0,1) 100%);',
        'Blue to black gradient'
      ],
      // custom html
      'custom2' => [
        '<div style="width:200px;">
          custom html demo 😍
        </div>',
        'Custom HTML demo :)'
      ],
    ]);

 

  • Like 2
  • Thanks 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...