Skip to content
Open
Changes from all commits
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
93 changes: 85 additions & 8 deletions packages/main/src/ColorPalette.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,12 +353,6 @@ class ColorPalette extends UI5Element {
if (isSpace(e)) {
e.preventDefault();
}

// Prevent Home/End keys from working in embedded mode - they only work in popup mode as per design
if (!this.popupMode && (isHome(e) || isEnd(e))) {
e.preventDefault();
e.stopPropagation();
}
}

handleSelection(target: ColorPaletteItem) {
Expand Down Expand Up @@ -410,16 +404,19 @@ class ColorPalette extends UI5Element {
}

if (this._isNext(e)) {
e.preventDefault();
e.stopPropagation();
this._focusFirstDisplayedColor();
} else if (isLeft(e)) {
e.preventDefault();
e.stopPropagation();
this._focusFirstAvailable(
() => this._focusLastRecentColor(),
() => this._focusMoreColors(),
() => this._focusLastDisplayedColor(),
);
} else if (isUp(e)) {
e.preventDefault();
e.stopPropagation();
this._focusFirstAvailable(
() => this._focusLastRecentColor(),
Expand All @@ -428,6 +425,13 @@ class ColorPalette extends UI5Element {
() => this._focusLastDisplayedColor(),
);
} else if (isEnd(e)) {
// Prevent Home/End keys from working in embedded mode - they only work in popup mode as per design
if (this._shouldPreventHomeEnd(e)) {
e.preventDefault();
e.stopPropagation();
return;
}
e.preventDefault();
e.stopPropagation();
this._focusFirstAvailable(
() => this._focusMoreColors(),
Expand All @@ -438,34 +442,61 @@ class ColorPalette extends UI5Element {

_onMoreColorsKeyDown(e: KeyboardEvent) {
if (isLeft(e)) {
e.preventDefault();
e.stopPropagation();
this._focusLastDisplayedColor();
} else if (isUp(e)) {
e.preventDefault();
e.stopPropagation();
this._focusFirstAvailable(
() => this._focusLastSwatchOfLastFullRow(),
() => this._focusLastDisplayedColor(),
);
} else if (this._isNext(e)) {
e.preventDefault();
e.stopPropagation();
this._focusFirstAvailable(
() => this._focusFirstRecentColor(),
() => this._focusDefaultColor(),
() => this._focusFirstDisplayedColor(),
);
} else if (isHome(e)) {
// Prevent Home/End keys from working in embedded mode - they only work in popup mode as per design
if (this._shouldPreventHomeEnd(e)) {
e.preventDefault();
e.stopPropagation();
return;
}
e.preventDefault();
e.stopPropagation();
this._focusFirstAvailable(
() => this._focusDefaultColor(),
() => this._focusFirstDisplayedColor(),
);
} else if (isEnd(e)) {
// Prevent Home/End keys from working in embedded mode - they only work in popup mode as per design
if (this._shouldPreventHomeEnd(e)) {
e.preventDefault();
e.stopPropagation();
return;
}
// More Colors button is typically the last element, so END key stays here
e.preventDefault();
e.stopPropagation();
}
}

_onColorContainerKeyDown(e: KeyboardEvent) {
const target = e.target as ColorPaletteItem;
const isLastSwatchInSingleRow = this._isSingleRow() && this._isLastSwatch(target, this.displayedColors);

// Prevent Home/End keys from working in embedded mode - they only work in popup mode as per design
if (this._shouldPreventHomeEnd(e)) {
e.preventDefault();
e.stopPropagation();
return;
}

if (this._isUpOrDownNavigatableColorPaletteItem(e)) {
this._currentlySelected = undefined;
}
Expand All @@ -476,6 +507,7 @@ class ColorPalette extends UI5Element {
}

if (this._isPrevious(e) && this._isFirstSwatch(target, this.displayedColors)) {
e.preventDefault();
e.stopPropagation();
this._focusFirstAvailable(
() => this._focusDefaultColor(),
Expand All @@ -487,26 +519,32 @@ class ColorPalette extends UI5Element {
} else if ((isRight(e) && this._isLastSwatch(target, this.displayedColors))
|| (isDown(e) && (this._isLastSwatchOfLastFullRow(target) || isLastSwatchInSingleRow))
) {
e.preventDefault();
e.stopPropagation();
this._focusFirstAvailable(
() => this._focusMoreColors(),
() => this._focusFirstRecentColor(),
() => this._focusDefaultColor(),
() => this._focusFirstDisplayedColor(),
);
} else if (isHome(e) && this._isFirstSwatch(target, this.displayedColors)) {
} else if (isHome(e) && this._isFirstSwatchInRow(target)) {
e.preventDefault();
e.stopPropagation();
this._focusFirstAvailable(
() => this._focusDefaultColor(),
() => this._focusMoreColors(),
() => this._focusFirstDisplayedColor(),
);
} else if (isEnd(e) && this._isLastSwatch(target, this.displayedColors)) {
} else if (isEnd(e) && this._isLastSwatchInRow(target)) {
e.preventDefault();
e.stopPropagation();
this._focusFirstAvailable(
() => this._focusMoreColors(),
() => this._focusDefaultColor(),
() => this._focusLastDisplayedColor(),
);
} else if (isEnd(e) && this._isSwatchInLastRow(target)) {
e.preventDefault();
e.stopPropagation();
this._focusLastDisplayedColor();
}
Expand All @@ -515,18 +553,27 @@ class ColorPalette extends UI5Element {
_onRecentColorsContainerKeyDown(e: KeyboardEvent) {
const target = e.target as ColorPaletteItem;

// Prevent Home/End keys from working in embedded mode - they only work in popup mode as per design
if (this._shouldPreventHomeEnd(e)) {
e.preventDefault();
e.stopPropagation();
return;
}

if (this._isUpOrDownNavigatableColorPaletteItem(e)) {
this._currentlySelected = undefined;
}

if (this._isNext(e) && this._isLastSwatch(target, this.recentColorsElements)) {
e.preventDefault();
e.stopPropagation();
this._focusFirstAvailable(
() => this._focusDefaultColor(),
() => this._focusMoreColors(),
() => this._focusFirstDisplayedColor(),
);
} else if (this._isPrevious(e) && this._isFirstSwatch(target, this.recentColorsElements)) {
e.preventDefault();
e.stopPropagation();
this._focusFirstAvailable(
() => this._focusMoreColors(),
Expand All @@ -535,6 +582,7 @@ class ColorPalette extends UI5Element {
() => this._focusDefaultColor(),
);
} else if (isEnd(e)) {
e.preventDefault();
e.stopPropagation();
this._focusLastRecentColor();
}
Expand Down Expand Up @@ -569,6 +617,24 @@ class ColorPalette extends UI5Element {
return swatches && Boolean(swatches.length) && swatches[swatches.length - 1] === target;
}

/**
* Checks if the target swatch is the first swatch in its row.
* @private
*/
_isFirstSwatchInRow(target: ColorPaletteItem): boolean {
const index = this.displayedColors.indexOf(target);
return index >= 0 ? index % this.rowSize === 0 : false;
}

/**
* Checks if the target swatch is the last swatch in its row.
* @private
*/
_isLastSwatchInRow(target: ColorPaletteItem): boolean {
const index = this.displayedColors.indexOf(target);
return index >= 0 ? (index + 1) % this.rowSize === 0 || index === this.displayedColors.length - 1 : false;
}

/**
* Checks if the given color swatch is the last swatch of the last full row.
*
Expand All @@ -594,6 +660,17 @@ class ColorPalette extends UI5Element {
return index >= 0 && index >= this.displayedColors.length - lastRowSwatchesCount;
}

/**
* Checks if HOME/END navigation should be prevented in embedded mode.
* In embedded mode, HOME/END keys are blocked as they only work in popup mode per design.
* @private
* @param e The keyboard event to check
* @returns True if the event should be prevented, false otherwise
*/
_shouldPreventHomeEnd(e: KeyboardEvent): boolean {
return !this.popupMode && (isHome(e) || isEnd(e));
}

/**
* Helper to check if all displayed colors fit in a single row
* @private
Expand Down
Loading