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

[space-picker] First iteration #143

Merged
merged 7 commits into from
Nov 5, 2024
Merged

[space-picker] First iteration #143

merged 7 commits into from
Nov 5, 2024

Conversation

DmitrySharabin
Copy link
Member

@DmitrySharabin DmitrySharabin commented Oct 24, 2024

Partially addresses #123.

Usage examples

<space-picker></space-picker>
<space-picker value="oklab"></space-picker>
<space-picker spaces="oklch, p3, srgb"></space-picker>
<space-picker spaces="oklch, p3, srgb" value="p3"></space-picker>
<space-picker onspacechange="this.nextElementSibling.textContent = this.value"></space-picker>
<output></output>

Copy link

netlify bot commented Oct 24, 2024

Deploy Preview for color-elements ready!

Name Link
🔨 Latest commit 42ae461
🔍 Latest deploy log https://app.netlify.com/sites/color-elements/deploys/6729f4419f15120008fb1c19
😎 Deploy Preview https://deploy-preview-143--color-elements.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

default: () => Self.Color.spaces,
convert (value) {
// Drop non-existing spaces
let spaces = Object.fromEntries(Object.entries(value).filter(([id, space]) => space));
Copy link
Member

Choose a reason for hiding this comment

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

I don't think we should drop them, I think we should just not list them with a name. Just the id.

Copy link
Member

Choose a reason for hiding this comment

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

I.e. the name should default to the id.

Copy link
Member Author

Choose a reason for hiding this comment

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

Makes sense. Will fix it. Thanks!


static formAssociated = {
like: el => el._el.picker,
role: "combobox",
Copy link
Member

Choose a reason for hiding this comment

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

Doesn't this default to the element we specified in like? If not, it probably should.

Copy link
Member Author

@DmitrySharabin DmitrySharabin Oct 30, 2024

Choose a reason for hiding this comment

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

In our code, we try to default to computedRole. Unfortunately, it still isn't supported. It's probably under the flag in some browsers (not sure).

image

Copy link
Member

Choose a reason for hiding this comment

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

We should just use getComputedAccessibleNode() instead. It's not like we don't have access to this information at all.

Copy link
Member

Choose a reason for hiding this comment

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

(in Nude Element, in case that wasn't clear)

Copy link
Member Author

Choose a reason for hiding this comment

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

Got it. Thanks. I'll send a PR with the change.

like: el => el._el.picker,
role: "combobox",
valueProp: "value",
changeEvent: "spacechange",
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this use change?


value += "";

return Self.Color.Space.get(value);
Copy link
Member

Choose a reason for hiding this comment

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

This is again the same issue with the string/object props. We need a prop that contains your actual input as a string (value) and one that contains the color space object (likely space or selectedSpace).

this.value = this._el.picker.value;
}

this.dispatchEvent(new event.constructor(event.type, {...event}));
Copy link
Member

Choose a reason for hiding this comment

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

Doesn't defining the change event below take care of this?

@DmitrySharabin
Copy link
Member Author

Thank you so much for your review! Every comment is on point, and I'll try to address it ASAP.

- Use `value` for actual input (as a string) and `selectedSpace` for the corresponding space object
- Support custom grouping of color spaces via the `groups()` prop
- Don't drop non-existent spaces; replace them with the `{ id, name: id }` objects
- Expose the `valuechange` and `spacechange` events
- Retarget the `input` event
- In `formAssociated`, use `change` as the `changeEvent`
@DmitrySharabin
Copy link
Member Author

@LeaVerou, I addressed all your feedback (thank you for it). Here are the changes I made:

  • Use value for actual input (as a string) and selectedSpace for the corresponding space object
  • Support custom grouping of color spaces via the groups() prop
  • Don't drop non-existent spaces; replace them with the { id, name: id } objects
  • Expose the valuechange and spacechange events
  • Retarget the input event
  • In formAssociated, use change as the changeEvent

Here is an example of using a custom grouping (from your color palettes research):

<space-picker id="space_picker" spaces="oklch, p3, srgb" value = "p3"></space-picker>

<script type="module">
	space_picker.groups = (spaces) => {
		let ret = {polar: {}, rectangular: {}};
		for (let id in spaces) {
			let space = spaces[id];

			if (space.id !== id) {
				continue;
			}

			let isPolar = space.coords.h?.type === "angle";
			ret[isPolar ? "polar" : "rectangular"][id] = space;
		}
		return ret;
	};
</script>
image

Could you please take a look at this PR one more time?

Copy link
Member

@LeaVerou LeaVerou left a comment

Choose a reason for hiding this comment

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

  • Authors should not have to loop over spaces themselves in the grouping function, it doesn't seem like they get any benefit for the extra work, are there cases where the grouping depends on what other spaces are available? (and even if so, one can just check spaces?). I was thinking of something like a groupBy() function that just takes a single space as a param, and gets back a group name. groups could still exist as a computed prop.
  • Why do we need defaultGroups? That seems to exist purely for implementation convenience. No grouping is not just a single group, that implies a different UI.
  • We definitely need to pick up the design discussion around rawProps, the pattern keeps coming up.

@DmitrySharabin
Copy link
Member Author

Thank you so much for your feedback! I tried to address all the issues you pointed out. Namely:

  • Add the groupBy() prop that expects a space and returns the name of a group the space belongs to

Providing grouping looks like so now:

<space-picker id="space_picker" spaces="oklch, p3, srgb" value = "p3"></space-picker>

<script type="module">
	space_picker.groupBy = (space) => {
		let isPolar = space.coords.h?.type === "angle";
		return isPolar? "Polar" : "Rectangular";
	};
</script>
image
  • Related to the previous one: groups is now a computed prop returning an object with all the groups
  • Use different UI for cases with grouping and without

I hope I got everything right. If not, I'll be happy to fix it.

* Add the `groupBy()` prop that expects a space and returns the name of a group the space belongs to
* Related to the previous one: `groups` is now a computed prop returning an object with all the groups
* Use different UI for cases with grouping and without
Oops
Copy link
Member

@LeaVerou LeaVerou left a comment

Choose a reason for hiding this comment

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

Looks good! Please add a README, and add it to the list of upcoming components.

@DmitrySharabin
Copy link
Member Author

Looks good! Please add a README, and add it to the list of upcoming components.

Thank you! Will do.

@DmitrySharabin DmitrySharabin merged commit 1075f15 into main Nov 5, 2024
4 checks passed
@DmitrySharabin DmitrySharabin deleted the space-picker branch November 5, 2024 10:35
@DmitrySharabin
Copy link
Member Author

Done! It's alive and can be played with: https://elements.colorjs.io/src/space-picker/ 🎉

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.

2 participants