Skip to content

Allow color gradient in color swatch #1074

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

Open
wants to merge 2 commits into
base: v1/contrib
Choose a base branch
from

Conversation

bjarnef
Copy link
Contributor

@bjarnef bjarnef commented Apr 19, 2025

Description

This PR allow us to use CSS gradients (linear or radial) in color swatches, which we actually had a use-case for in the an Umbraco 13 project, but due a limitation in the approved color picker, we made a "hack" to solves this.
#1018

The color should be a valid CSS background (including linear/radial gradient), but the value could possible any string value with e.g. gradient or something else from a hex color.

Of course the core approved colors in CMS can still validate a hex color, but this change allow us to use gradient in a custom property editor.

Some CSS gradient examples:
https://uigradients.com/
https://cssgradient.io/
https://webgradients.com/
https://coolors.co/gradients

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Chore (minor updates related to the tooling or maintenance of the repository, does not impact compiled assets)

Motivation and context

It allow us to use CSS background colors as well and not only solid colors.
This could be useful in custom implementations in property editors, dashboards etc. and using specific frontend features like e.g. Tailwind CSS gradients:
https://tailscan.com/gradients

Furthermore some inspiration from Fluent UI library here:
https://react.fluentui.dev/?path=/docs/components-swatchpicker--docs&globals=storybook_fluentui-react-addon_theme:web-light#color-swatch-variants

How to test?

Screenshots (if appropriate)

image

image

Checklist

  • If my change requires a change to the documentation, I have updated the documentation in this pull request.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.

@Copilot Copilot AI review requested due to automatic review settings April 19, 2025 15:00
Copy link

Hi there @bjarnef, thank you for this contribution! 👍

While we wait for the team to have a look at your work, we wanted to let you know about that we have a checklist for some of the things we will consider during review:

  • It's clear what problem this is solving, there's a connected issue or a description of what the changes do and how to test them
  • The automated tests all pass (see "Checks" tab on this PR)
  • The level of security for this contribution is the same or improved
  • The level of performance for this contribution is the same or improved
  • Avoids creating breaking changes; note that behavioral changes might also be perceived as breaking
  • If this is a new feature, Umbraco HQ provided guidance on the implementation beforehand
  • 💡 The contribution looks original and the contributor is presumably allowed to share it

Don't worry if you got something wrong. We like to think of a pull request as the start of a conversation, we're happy to provide guidance on improving your contribution.

If you realize that you might want to make some changes then you can do that by adding new commits to the branch you created for this work and pushing new commits. They should then automatically show up as updates to this pull request.

Thanks, from your friendly Umbraco GitHub bot 🤖 🙂

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds support for CSS gradients in color swatches by enhancing the story and component implementation.

  • Introduces a new gradients array and updates the story to use an additional "color" attribute.
  • Updates the swatch element’s style from "background-color" to "background" to properly render gradient values.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
packages/uui-color-swatches/lib/uui-color-swatches.story.ts Updated story to include both solid color and gradient swatches, adding a new "gradients" array and a "color" binding.
packages/uui-color-swatch/lib/uui-color-swatch.element.ts Changed the style property from "background-color" to "background" for improved gradient support.
Comments suppressed due to low confidence (2)

packages/uui-color-swatch/lib/uui-color-swatch.element.ts:132

  • Verify that switching from 'background-color' to 'background' accommodates both solid colors and gradient values as intended, ensuring consistent rendering across different swatch types.
style="background: var(--uui-swatch-color, ${this.color ?? this.value})"

packages/uui-color-swatches/lib/uui-color-swatches.story.ts:22

  • [nitpick] The gradient with label 'Disco Club' uses 'avocado' as its value, which might be confused with an existing solid color swatch. Consider using a more distinct value identifier for clarity.
    { label: 'Disco Club', value: 'avocado', color:

const label = typeof swatch === 'string' ? swatch : swatch.label;
const value = typeof swatch === 'string' ? swatch : swatch.value;
const color = typeof swatch === 'string' ? swatch : swatch.color;
Copy link
Preview

Copilot AI Apr 19, 2025

Choose a reason for hiding this comment

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

Consider ensuring that all swatch objects intended to display gradients explicitly include a 'color' property, as using a string fallback may lead to unexpected behavior when gradients are desired.

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant