Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ui5-color-palette): initial implementation #2731

Merged
merged 16 commits into from
Feb 11, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions packages/base/src/types/CSSColor.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/main/bundle.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import Button from "./dist/Button.js";
import Card from "./dist/Card.js";
import Carousel from "./dist/Carousel.js";
import CheckBox from "./dist/CheckBox.js";
import ColorPalette from "./dist/ColorPalette.js";
import ComboBox from "./dist/ComboBox.js";
import DatePicker from "./dist/DatePicker.js";
import DateRangePicker from "./dist/DateRangePicker.js";
Expand Down
23 changes: 23 additions & 0 deletions packages/main/src/ColorPalette.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<div class="ui5-cp-swatch-container"
@click={{_onclick}}
tsanislavgatev marked this conversation as resolved.
Show resolved Hide resolved
@keyup={{_onkeyup}}
tsanislavgatev marked this conversation as resolved.
Show resolved Hide resolved
role="region"
aria-label="{{colorContainerLabel}}">
{{#each colorsArray}}
<div class="ui5-cp-swatch-row">
{{#each this}}
<div
id="color-swatch-{{this.id}}"
class="ui5-cp-swatch"
style="background-color: {{this.value}}"
value="{{this.value}}"
tabindex="0"
role="button"
aria-label="Color {{this.id}}: {{this.value}}"
title="Color {{this.id}}: {{this.value}}"
>
</div>
{{/each}}
</div>
{{/each}}
</div>
204 changes: 204 additions & 0 deletions packages/main/src/ColorPalette.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
import UI5Element from "@ui5/webcomponents-base/dist/UI5Element.js";
import litRender from "@ui5/webcomponents-base/dist/renderer/LitRenderer.js";
import ColorPaletteTemplate from "./generated/templates/ColorPaletteTemplate.lit.js";
import ItemNavigation from "@ui5/webcomponents-base/dist/delegate/ItemNavigation.js";
import ColorPaletteEntry from "./ColorPaletteEntry.js";
import CSSColor from "@ui5/webcomponents-base/dist/types/CSSColor.js";
import ItemNavigationBehavior from "@ui5/webcomponents-base/dist/types/ItemNavigationBehavior.js";
import {
isSpace,
isEnter,
} from "@ui5/webcomponents-base/dist/Keys.js";

// Styles
import ColorPaletteCss from "./generated/themes/ColorPalette.css.js";

/**
* @public
*/
const metadata = {
tag: "ui5-color-palette",
managedSlots: true,
properties: /** @lends sap.ui.webcomponents.main.ColorPalette.prototype */ {
/**
* @type {CSSColor}
* @public
tsanislavgatev marked this conversation as resolved.
Show resolved Hide resolved
*/
value: {
type: CSSColor
}
},
slots: /** @lends sap.ui.webcomponents.main.ColorPalette.prototype */ {
/**
* @type {HTMLElement[]}
* @slot
* @public
*/
"default": {
propertyName: "colors",
type: HTMLElement,
invalidateOnChildChange: true,
},
},
events: /** @lends sap.ui.webcomponents.main.ColorPalette.prototype */ {
change: {
details: {
color: {
type: "String"
}
}
}
},
};

/**
* @class
*
* <h3 class="comment-api-title">Overview</h3>
*
tsanislavgatev marked this conversation as resolved.
Show resolved Hide resolved
*
* <h3>Usage</h3>
*
* For the <code>ui5-color-palette</code>
* <h3>ES6 Module Import</h3>
*
* <code>import @ui5/webcomponents/dist/ColorPalette.js";</code>
*
* @constructor
ilhan007 marked this conversation as resolved.
Show resolved Hide resolved
* @author SAP SE
* @alias sap.ui.webcomponents.main.ColorPalette
ilhan007 marked this conversation as resolved.
Show resolved Hide resolved
* @extends UI5Element
* @tagname ui5-color-palette
* @public
*/
class ColorPalette extends UI5Element {
static get metadata() {
return metadata;
}

static get render() {
return litRender;
}

static get styles() {
return ColorPaletteCss;
}

static get template() {
return ColorPaletteTemplate;
}

static get dependencies() {
return [ColorPaletteEntry];
}

static async onDefine() {
tsanislavgatev marked this conversation as resolved.
Show resolved Hide resolved

}

constructor() {
tsanislavgatev marked this conversation as resolved.
Show resolved Hide resolved
super();
}

onBeforeRendering() {
this._colorsArray = this.colors;
}

onAfterRendering() {
this._itemNavigation = new ItemNavigation(this, {
tsanislavgatev marked this conversation as resolved.
Show resolved Hide resolved
getItemsCallback: () => Array.from(this.getDomRef().getElementsByClassName("ui5-cp-swatch")),
rowSize: 5,
behavior: ItemNavigationBehavior.Cyclic,
});

this._itemNavigation.onOverflowBottomEdge = this.onOverflowBottomEdge;
this._itemNavigation.onOverflowTopEdge = this.onOverflowTopEdge;
}

selectColor(target) {
tsanislavgatev marked this conversation as resolved.
Show resolved Hide resolved
target.focus();
this._itemNavigation.update(target);

this.value = target.getAttribute("value");

this.fireEvent("change", {
color: this.value,
});
}

onOverflowBottomEdge(event) {
const newIndex = this.currentIndex % this.rowSize;
this.currentIndex = newIndex + 1 >= this.rowSize ? 0 : newIndex + 1;

return;
tsanislavgatev marked this conversation as resolved.
Show resolved Hide resolved
}

onOverflowTopEdge(event) {
const items = this._getItems();
const firstIndex = -5;
let rowsMultiplier;

if(this.currentIndex === firstIndex) {
rowsMultiplier = Math.floor((items.length + (this.rowSize + 2)) / this.rowSize);
} else {
rowsMultiplier = Math.floor((items.length + (this.rowSize + this.currentIndex + 2)) / this.rowSize);
}

const newIndex = this.currentIndex + rowsMultiplier * this.rowSize;

this.currentIndex = newIndex - 1;

return;
tsanislavgatev marked this conversation as resolved.
Show resolved Hide resolved
}

_onclick(event) {
const target = event.target;
event.preventDefault();
tsanislavgatev marked this conversation as resolved.
Show resolved Hide resolved
event.stopPropagation();

if (target.classList.contains("ui5-cp-swatch")){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you move the click event binding to the <div class="ui5-cp-swatch-row"> you will not need this check

this.selectColor(target);
}
}

_onkeyup(event) {
const target = event.target;
event.preventDefault();
tsanislavgatev marked this conversation as resolved.
Show resolved Hide resolved
event.stopPropagation();

if (isEnter(event) || isSpace(event)) {
this.selectColor(target);
}
}


get colorsArray() {
const elementsPerRow = 5;
const maxAllowedColors = 15;
const rowsArray = this._colorsArray.slice(0, maxAllowedColors).reduce((resultArray, item, index) => {
const chunkIndex = Math.floor(index / elementsPerRow);

if(!resultArray[chunkIndex]) {
resultArray[chunkIndex] = [];
}

resultArray[chunkIndex].push({ value: item.value, id: index });

return resultArray;
}, []);

return rowsArray;
}

get colorContainerLabel() {
return "labelinho";
tsanislavgatev marked this conversation as resolved.
Show resolved Hide resolved
}

get colorLabel() {
return "color";
tsanislavgatev marked this conversation as resolved.
Show resolved Hide resolved
}
}

ColorPalette.define();

export default ColorPalette;
60 changes: 60 additions & 0 deletions packages/main/src/ColorPaletteEntry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import UI5Element from "@ui5/webcomponents-base/dist/UI5Element.js";
import CSSColor from "@ui5/webcomponents-base/dist/types/CSSColor.js";

/**
* @public
*/
const metadata = {
tag: "ui5-color-palette-entry",
managedSlots: true,
properties: /** @lends sap.ui.webcomponents.main.ColorPaletteEntry.prototype */ {
/**
* Defines the value of the <code>ui5-color-palette</code> inside an HTML Form element when this <code>ui5-color-palette-entry</code> is presented.
tsanislavgatev marked this conversation as resolved.
Show resolved Hide resolved
*
* @type {CSSColor}
* @public
*/
value: {
type: CSSColor,
},

/**
* Defines the stable selector that you can use via getStableDomRef method.
* @public
* @since 1.0.0-rc.29
tsanislavgatev marked this conversation as resolved.
Show resolved Hide resolved
*/
stableDomRef: {
type: String,
},
},
slots: {
"default": {
type: Node,
},
},
events: /** @lends sap.ui.webcomponents.main.ColorPaletteEntry.prototype */ {},
};

/**
* @class
*
* <h3 class="comment-api-title">Overview</h3>
*
* The <code>ui5-color-palette</code> component defines the content of an color in the <code>ui5-color-palette</code>.
*
* @constructor
* @author SAP SE
ilhan007 marked this conversation as resolved.
Show resolved Hide resolved
* @alias sap.ui.webcomponents.main.ColorPaletteEntry
* @extends sap.ui.webcomponents.base.UI5Element
* @tagname ui5-color-palette
* @public
*/
class ColorPaletteEntry extends UI5Element {
static get metadata() {
return metadata;
}
}

ColorPaletteEntry.define();

export default ColorPaletteEntry;
25 changes: 25 additions & 0 deletions packages/main/src/themes/ColorPalette.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
.ui5-cp-swatch {
height: var(--_ui5_color-palette-swatch-height);
width: var(--_ui5_color-palette-swatch-height);
border: 1px solid var(--sapContent_ForegroundBorderColor);
border-radius: 0.25rem;
display: inline-block;
margin: var(--_ui5_color-palette-swatch-margin);
}

.ui5-cp-swatch:hover {
height: var(--_ui5_color-palette-swatch-hover-height);
width: var(--_ui5_color-palette-swatch-hover-height);
margin: var(--_ui5_color-palette-swatch-hover-margin);
}

.ui5-cp-swatch-row {
display: flex;
padding: 0.3125rem 0.6875rem;
justify-content: flex-start;
}

.ui5-cp-swatch-container {
tsanislavgatev marked this conversation as resolved.
Show resolved Hide resolved
display: flex;
flex-flow: column wrap;
}
7 changes: 7 additions & 0 deletions packages/main/src/themes/base/ColorPalette-parameters.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
:root {
--_ui5_color-palette-swatch-height: 1.75rem;
--_ui5_color-palette-swatch-container-padding: 0.3125rem 0.6875rem;
--_ui5_color-palette-swatch-hover-height: 2.25rem;
--_ui5_color-palette-swatch-margin: calc(var(--_ui5_color-palette-swatch-hover-height) - var(--_ui5_color-palette-swatch-height));
--_ui5_color-palette-swatch-hover-margin: calc(var(--_ui5_color-palette-swatch-margin) / 2);
}
4 changes: 4 additions & 0 deletions packages/main/src/themes/base/sizes-parameters.css
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@
--_ui5_checkbox_inner_width_height: var(--_ui5_checkbox_compact_inner_size);
--_ui5_checkbox_icon_size: .75rem;

/* ColorPalette */
--_ui5_color-palette-swatch-height: 1.25rem;
--_ui5_color-palette-swatch-container-padding: 0.1875rem 0.8125rem;
--_ui5_color-palette-swatch-hover-height: 1.65rem;

/* Custom List Item */
--_ui5_custom_list_item_height: 2rem;
Expand Down
1 change: 1 addition & 0 deletions packages/main/src/themes/sap_belize/parameters-bundle.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
@import "./Carousel-parameters.css";
@import "../base/Card-parameters.css";
@import "../base/CheckBox-parameters.css";
@import "../base/ColorPalette-parameters.css";
@import "../base/GroupHeaderListItem-parameters.css";
@import "./Input-parameters.css";
@import "./InputIcon-parameters.css";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
@import "./Carousel-parameters.css";
@import "./Card-parameters.css";
@import "./CheckBox-parameters.css";
@import "../base/ColorPalette-parameters.css";
@import "./DatePicker-parameters.css";
@import "./DayPicker-parameters.css";
@import "./Dialog-parameters.css";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
@import "./Carousel-parameters.css";
@import "./Card-parameters.css";
@import "./CheckBox-parameters.css";
@import "../base/ColorPalette-parameters.css";
@import "./DatePicker-parameters.css";
@import "./DayPicker-parameters.css";
@import "./Dialog-parameters.css";
Expand Down
1 change: 1 addition & 0 deletions packages/main/src/themes/sap_fiori_3/parameters-bundle.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
@import "../base/Card-parameters.css";
@import "../base/Carousel-parameters.css";
@import "./CheckBox-parameters.css";
@import "../base/ColorPalette-parameters.css";
@import "../base/DatePicker-parameters.css";
@import "./DayPicker-parameters.css";
@import "../base/Dialog-parameters.css";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
@import "../base/Card-parameters.css";
@import "../base/Carousel-parameters.css";
@import "./CheckBox-parameters.css";
@import "../base/ColorPalette-parameters.css";
@import "../base/DatePicker-parameters.css";
@import "./DayPicker-parameters.css";
@import "../base/Dialog-parameters.css";
Expand Down
Loading