Skip to content

Commit 416dfe9

Browse files
feat: add support for custom CSS + HTML
1 parent 43cf0b9 commit 416dfe9

File tree

7 files changed

+106
-37
lines changed

7 files changed

+106
-37
lines changed

.github/workflows/releases.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Releases
2+
on:
3+
push:
4+
branches:
5+
- main
6+
7+
jobs:
8+
changelog:
9+
runs-on: ubuntu-latest
10+
11+
steps:
12+
- uses: actions/checkout@v2
13+
- name: conventional Changelog Action
14+
id: changelog
15+
uses: TriPSs/[email protected]
16+
with:
17+
github-token: ${{ secrets.github_token }}
18+
19+
- name: create release
20+
uses: actions/create-release@v1
21+
if: ${{ steps.changelog.outputs.skipped == 'false' }}
22+
env:
23+
GITHUB_TOKEN: ${{ secrets.github_token }}
24+
with:
25+
tag_name: ${{ steps.changelog.outputs.tag }}
26+
release_name: ${{ steps.changelog.outputs.tag }}
27+
body: ${{ steps.changelog.outputs.clean_changelog }}

InputfieldMarkup.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,25 @@
22
<?php
33
foreach ($f->colors() as $col) {
44
$selected = ($value == $col->name) ? "selected" : "";
5-
echo "<svg class='rcp-color $selected' data-col='{$col->name}'>
6-
<rect style='fill:{$col->css}' title='{$col->tooltip}' uk-tooltip />
7-
</svg>";
5+
6+
// prepare html and style tag
7+
// If provided a color like #ff0000 or rgba(0,0,0) we prepend the
8+
// background-color property. Otherwise we use it as is.
9+
$style = $col->css;
10+
if (strpos($style, "<") === 0) {
11+
$html = $style;
12+
} else {
13+
if (!strpos($style, ":")) $style = "background-color:$style";
14+
$html = "<div style='$style'></div>";
15+
}
16+
17+
// output wrapper + html
18+
echo "<div
19+
class='rcp-color $selected'
20+
data-color='{$col->name}'
21+
title='{$col->label}' uk-tooltip
22+
>$html</div>";
823
}
924
?>
10-
<input type="text" id="<?= $f->id ?>" class="uk-input" name="<?= $f->name ?>" value="<?= $f->value ?>">
25+
<input type="text" id="<?= $f->id ?>" class="uk-input" name="<?= $f->name ?>" value="<?= $value ?>">
1126
</div>

README.md

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# RockColorPicker
22

3-
Super simple color picker for ProcessWire:
3+
Super simple color (and custom HTML) picker for ProcessWire:
44

5-
<img src=https://i.imgur.com/VYv3Mbz.png height=150>
5+
<img src=https://i.imgur.com/701Jj9C.png height=150>
66

77
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).
88

@@ -30,6 +30,22 @@ $wire->addHookBefore(
3030
'green' => ['#00ff00', 'This is green'],
3131
'blue' => ['#0000ff', 'This is blue'],
3232
]);
33+
34+
// custom CSS + HTML example
35+
$event->object->setColors('site_bgcolor', [
36+
// custom css
37+
'custom' => [
38+
'background: rgb(34,0,255); background: linear-gradient(90deg, rgba(34,0,255,1) 0%, rgba(0,0,0,1) 100%);',
39+
'Blue to black gradient'
40+
],
41+
// custom html
42+
'custom2' => [
43+
'<div style="width:200px;">
44+
custom html demo 😍
45+
</div>',
46+
'Custom HTML demo :)'
47+
],
48+
]);
3349
}
3450
);
3551
```
@@ -38,11 +54,11 @@ $wire->addHookBefore(
3854

3955
Formatted value:
4056

41-
<img src=https://i.imgur.com/sOBc18h.png width=280>
57+
<img src=https://imgur.com/E07skYE.png height=130>
4258

4359
Unformatted value:
4460

45-
<img src=https://i.imgur.com/StnDbWm.png width=400>
61+
<img src=https://i.imgur.com/whrWc67.png height=280>
4662

4763
## Why is there no GUI for setup?
4864

RockColorPicker.css

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,21 @@
66
.InputfieldRockColorPicker input {
77
display: none;
88
}
9-
svg.rcp-color {
10-
width: 40px;
11-
height: 40px;
9+
div.rcp-color {
1210
outline: 1px solid #afafaf;
1311
cursor: pointer;
1412
}
15-
svg.rcp-color.selected {
13+
.rcp-color.selected {
1614
outline: 5px solid #afafaf;
1715
}
18-
svg.rcp-color rect {
19-
width: 100%;
20-
height: 100%;
16+
/* set the size of the inner element */
17+
.rcp-color > * {
18+
width: 40px;
19+
height: 40px;
20+
}
21+
/* make text content centered by default */
22+
.rcp-color > * {
23+
display: flex;
24+
align-items: center;
25+
justify-content: center;
2126
}

RockColorPicker.js

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,33 @@
1-
$(document).on("click", ".InputfieldRockColorPicker svg", function () {
2-
// using UIkit JS because jquery fails with addclass on svg
3-
// see https://stackoverflow.com/a/10257755/6370411
4-
let util = UIkit.util;
5-
let el = util.$(this);
6-
let parent = el.parentNode;
7-
let $li = $(this).closest(".Inputfield");
8-
let input = util.$("input", parent);
9-
let remove = util.hasClass(el, "selected");
10-
util.each(util.$$("svg", parent), function (el) {
11-
util.removeClass(el, "selected");
1+
$(document).on("click", ".InputfieldRockColorPicker .rcp-color", function () {
2+
let $color = $(this).closest(".rcp-color");
3+
let $colors = $color.closest(".rcp-colors");
4+
let $li = $color.closest(".Inputfield");
5+
let $input = $li.find("input");
6+
7+
// set flag that defines wether we can remove selected state or not
8+
let remove =
9+
$color.hasClass("selected") && !$li.hasClass("InputfieldStateRequired");
10+
11+
// unselect all items
12+
$.each($colors.find(".rcp-color"), function (i, el) {
13+
$(el).removeClass("selected");
1214
});
13-
$(this).closest(".Inputfield").addClass("InputfieldStateChanged");
14-
if (remove && !$li.hasClass("InputfieldStateRequired")) {
15-
util.attr(input, "value", "");
15+
16+
// set the new selected item
17+
if (remove) {
18+
$input.val("");
1619
} else {
17-
util.addClass(el, "selected");
18-
util.attr(input, "value", util.data(el, "col"));
20+
$color.addClass("selected");
21+
$input.val($color.data("color"));
1922
}
20-
$(input).change(); // trigger change event (important for rockpagebuilder)
23+
24+
// trigger change on the inputfield
25+
$li.addClass("InputfieldStateChanged");
26+
$input.change(); // for rockpagebuilder
2127
});
2228
$(document).bind("keypress", function (e) {
2329
// support space+enter to change color
2430
if (e.which == 32 || e.which == 13) {
25-
$(":focus").closest("svg.rcp-color").click();
31+
$(":focus").closest(".rcp-color").click();
2632
}
2733
});

RockColorPicker.module.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static function getModuleInfo()
1818
{
1919
return [
2020
'title' => 'RockColorPicker',
21-
'version' => '1.0.0',
21+
'version' => '1.1.0',
2222
'summary' => 'Main RockColorPicker Module',
2323
'autoload' => false,
2424
'singular' => true, // sic
@@ -74,9 +74,6 @@ public function setColors($field, $colors, $force = false)
7474
$item->name = $name;
7575
$item->css = $data[0];
7676
$item->label = count($data) > 1 ? $data[1] : '';
77-
$item->tooltip = $item->label
78-
. ($item->label ? " - " : "")
79-
. $item->css;
8077
$arr->set($name, $item);
8178
}
8279
$this->colors->set($field, $arr);

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"version": "1.0.0"
3+
}

0 commit comments

Comments
 (0)