Skip to content

Commit 95435df

Browse files
authored
Merge pull request #111 from AdobeDocs/barranca/colorpicker
Barranca/colorpicker
2 parents e78ec6c + ce244d7 commit 95435df

File tree

9 files changed

+509
-6
lines changed

9 files changed

+509
-6
lines changed

gatsby-config.js

+10
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,16 @@ module.exports = {
498498
},
499499
],
500500
},
501+
{
502+
title: "Built-in UI Components",
503+
path: "references/ui-components/color-picker.md",
504+
pages: [
505+
{
506+
title: "Color Picker",
507+
path: "references/ui-components/color-picker.md",
508+
},
509+
],
510+
},
501511
{
502512
title: "Manifest Schema",
503513
description: "Manifest schema",

src/pages/guides/develop/how_to/use_color.md

+155
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ keywords:
1616
- Text Color
1717
- HEX Color
1818
- RGB Color
19+
- Color Picker
1920
title: Use Color
2021
description: Use Color.
2122
contributors:
@@ -127,3 +128,157 @@ ellipse.stroke = editor.makeStroke({
127128
Naming conventions
128129

129130
Please note that Adobe Express uses the terms **make** and **create** to distinguish between plain objects and live document objects. You `makeColorFill()`, but `createEllipse()`.
131+
132+
## Use the Color Picker
133+
134+
Adobe Express includes a native Color Picker, with special features such as Recommended Swatches, Eyedropper, Themes, Library and Brand colors. The Color Picker is available also to add-ons, you can invoke it using the [`addOnUISdk.app.showColorPicker()`](../../../references/addonsdk/addonsdk-app.md#showcolorpicker) method.
135+
136+
#### Benefits
137+
138+
- It simplifies the process of selecting a color, bypassing the Browser's color picker.
139+
- It's in sync with any swatches or Brand colors defined in Adobe Express.
140+
- It will evolve with Adobe Express, providing a consistent color picking experience across different parts of the application.
141+
142+
The `showColorPicker()` method accepts a reference to an HTML element as its first argument, which will become the color picker's anchor element. The picker will be positioned relative to this element, based on the placement options available in the `ColorPickerPlacement` enum; additionally, the anchor will receive a custom `"colorpicker-color-change"` event when the color changes and a `"colorpicker-close"` event when it is closed.
143+
144+
The `showColorPicker()` method requires an HTML element as its anchor point. Here's how it works:
145+
146+
1. **Anchor Element**
147+
148+
- Pass an HTML element reference as the first argument.
149+
- The color picker will position itself relative to this element.
150+
- Use the `ColorPickerPlacement` enum to control positioning.
151+
152+
2. **Event Handling**
153+
154+
- The anchor element receives two custom events:
155+
- `"colorpicker-color-change"`: Fires when a new color is selected.
156+
- `"colorpicker-close"`: Fires when the picker is closed.
157+
158+
### Example: Show the Color Picker
159+
160+
<CodeBlock slots="heading, code" repeat="2" languages="js, html"/>
161+
162+
#### ui/index.js
163+
164+
```js
165+
import addOnUISdk, {
166+
ColorPickerEvents,
167+
ColorPickerPlacement,
168+
} from "https://new.express.adobe.com/static/add-on-sdk/sdk.js";
169+
170+
addOnUISdk.ready.then(async () => {
171+
// Get the button element
172+
const colorPickerButton = document.getElementById("colorPicker");
173+
174+
// Add a click event listener to the button to show the color picker
175+
colorPickerButton.addEventListener("click", () => {
176+
addOnUISdk.app.showColorPicker(colorPickerButton, {
177+
// The title of the color picker
178+
title: "Awesome Color Picker",
179+
// The placement of the color picker
180+
placement: ColorPickerPlacement.left,
181+
// Whether the eyedropper hides the color picker
182+
eyedropperHidesPicker: true,
183+
// The initial color of the color picker
184+
initialColor: 0x0000ff,
185+
// Whether the alpha channel is disabled
186+
disableAlphaChannel: false,
187+
});
188+
});
189+
190+
// Add a listener for the colorpicker-color-change event
191+
addOnUISdk.app.on(ColorPickerEvents.ColorChange, (event) => {
192+
// Get the color from the event
193+
console.log(event.detail.color);
194+
// e.g., "#F0EDD8FF" in HEX (RRGGBBAA) format
195+
});
196+
197+
// Add a listener for the colorpicker-close event
198+
colorPickerButton.addEventListener(ColorPickerEvents.close, (event) => {
199+
console.log(event.type); // "colorpicker-close"
200+
});
201+
});
202+
```
203+
204+
#### index.html
205+
206+
```html
207+
<button id="colorPicker">Show the Color Picker</button>
208+
```
209+
210+
Please note that the color returned by the `colorpicker-color-change` event is always a string in HEX format—with or without an alpha value, e.g., `#F0EDD8FF` or `#F0EDD8` depending on the `disableAlphaChannel` option.
211+
212+
### Example: Hide the Color Picker
213+
214+
You can decide to hide picker UI e.g., after a certain amount of time.
215+
216+
```js
217+
colorPickerButton.addEventListener("click", () => {
218+
addOnUISdk.app.showColorPicker(colorPickerButton, {
219+
/* ... */
220+
});
221+
setTimeout(() => {
222+
console.log("Hiding the Color Picker after 10 seconds");
223+
addOnUISdk.app.hideColorPicker();
224+
}, 10000);
225+
});
226+
```
227+
228+
### Example: Use the color
229+
230+
You can use any HTML element as the color picker's anchor element; in the example below, we're using a `<div>` element to display a color swatch.
231+
232+
<CodeBlock slots="heading, code" repeat="2" languages="html, js"/>
233+
234+
#### index.html
235+
236+
```html
237+
<style>
238+
#color-display {
239+
width: 30px;
240+
height: 30px;
241+
border: 1px solid black;
242+
border-radius: 4px;
243+
background-color: white;
244+
}
245+
</style>
246+
<body>
247+
<div id="color-display"></div>
248+
</body>
249+
```
250+
251+
#### index.js
252+
253+
```js
254+
addOnUISdk.ready.then(async () => {
255+
const colorDisplay = document.getElementById("color-display");
256+
257+
colorDisplay.addEventListener("click", () => {
258+
addOnUISdk.app.showColorPicker(colorDisplay, {
259+
title: "Color Picker 1",
260+
placement: ColorPickerPlacement.left,
261+
eyedropperHidesPicker: true,
262+
});
263+
});
264+
265+
colorDisplay.addEventListener(ColorPickerEvents.colorChange, (event) => {
266+
// Update the color swatch display in the UI
267+
colorDisplay.style.backgroundColor = event.detail.color;
268+
});
269+
});
270+
```
271+
272+
To use the picked color in the Document Sandbox, you can use the [`colorUtils.fromHex()`](../../../references/document-sandbox/document-apis/classes/ColorUtils.md#fromhex) method, which converts the HEX color string to a [`Color`](../../../references/document-sandbox/document-apis/interfaces/Color.md) object.
273+
274+
```js
275+
// sandbox/code.js
276+
const color = colorUtils.fromHex(event.detail.color); // 👈 A Color object
277+
278+
// Use the color in the Document Sandbox, for example:
279+
let selection = editor.context.selection;
280+
if (selection.length === 1 && selection[0].type === "Text") {
281+
const textContentModel = selection[0].fullContent;
282+
textContentModel.applyCharacterStyles({ color }); // 👈 Using the color
283+
}
284+
```

src/pages/guides/getting_started/quickstart.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ The **[Adobe Express add-on CLI](dev_tooling.md#using-the-cli) (Command Line Int
4949
Open your terminal and run the following command:
5050

5151
```bash
52-
npx @adobe/create-ccweb-add-on hello-world --template javascript-with-document-sandbox
52+
npx @adobe/create-ccweb-add-on hello-world --template javascript-with-document-sandbox
5353
```
5454

5555
This command will create a new add-on based on pure JavaScript with Document Sandbox support (the set of APIs that allow you to interact with Adobe Express documents).
@@ -71,13 +71,13 @@ Please run this command to clear the `npx` cache and ensure the latest version o
7171

7272
```bash
7373
npx clear-npx-cache
74-
npx @adobe/create-ccweb-add-on hello-world
74+
npx @adobe/create-ccweb-add-on hello-world
7575
```
7676

7777
The above may prove useful when updated versions of the CLI are released. If you want to read each individual CLI command manual page, run them via `npx` with the `--help` flag, for example:
7878

7979
```bash
80-
npx @adobe/ccweb-add-on-scripts start --help
80+
npx @adobe/ccweb-add-on-scripts start --help
8181
```
8282

8383
## Step 2: Build and start your add-on
@@ -134,9 +134,9 @@ For simplicity's sake, this Quickstart guide covers the document creation method
134134

135135
- Once clicked, a modal will appear where you will provide the URL of your locally hosted add-on.
136136

137-
**Note:** Use the default `https://localhost:5241` supplied unless you are intentionally using a different port.
137+
**Note:** Use the default `https://localhost:5241` supplied unless you are intentionally using a different port.
138138

139-
Select the *I understand the risks of loading an add-on from an external server* checkbox and press the **Connect** button.
139+
Select the _I understand the risks of loading an add-on from an external server_ checkbox and press the **Connect** button.
140140

141141
![Add-on connect modal](./img/connect-modal-v2.png)
142142

@@ -170,7 +170,7 @@ You can continue to update your code while your add-on is running, and the add-o
170170

171171
**Manifest updates**<br/>
172172

173-
Any changes to the `manifest.json` will *require a manual reload of your add-on*. The **Add-on Development** panel will indicate this in the log messages, and the **Refresh** button can be used to reload the add-on directly within Adobe Express. You can try this by updating the `name` field in the `src/manifest.json` file of your running add-on from "Hello World" to, say, **"Draw Rectangle"**.
173+
Any changes to the `manifest.json` will _require a manual reload of your add-on_. The **Add-on Development** panel will indicate this in the log messages, and the **Refresh** button can be used to reload the add-on directly within Adobe Express. You can try this by updating the `name` field in the `src/manifest.json` file of your running add-on from "Hello World" to, say, **"Draw Rectangle"**.
174174

175175
![manifest update](./img/manifest-update-v2.png)
176176

src/pages/references/addonsdk/addonsdk-app.md

+79
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,85 @@ async function showInputDialog() {
254254

255255
See the use case implementations for an example of the [custom modal dialog](../../guides/develop/how_to/modal_dialogs.md#custom-dialog).
256256

257+
### showColorPicker()
258+
259+
Shows the Adobe Express color picker based on specific options passed in.
260+
261+
#### Signature
262+
263+
`showColorPicker(anchorElement: HTMLElement, options?: ColorPickerOptions): Promise<void>;`
264+
265+
#### Parameters
266+
267+
| Name | Type | Description |
268+
| --------------- | -------------------- | ------------------------------------------------: |
269+
| `anchorElement` | `HTMLElement` | The element to anchor the color picker to. |
270+
| `options` | `ColorPickerOptions` | The optional options to pass to the color picker. |
271+
272+
##### `ColorPickerOptions`
273+
274+
| Name | Type | Description |
275+
| ------------------------ | -----------------------------------------------------------------: | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: |
276+
| `title?` | `string` | Label header/title for the color picker. Default value: `""` |
277+
| `initialColor?` | `number` | Default/starting color when you open the color picker for the first time on a `anchorElement`. When you have already changed the color with this picker, then the next time you open the picker, the last selected color will be the starting color. Default: `0xFFFFFF` (white) |
278+
| `placement?` | `object` [ColorPickerPlacement](./addonsdk-constants.md#constants) | Placement of the popover with respect to the anchor element (default: `"left"`). |
279+
| `eyedropperHidesPicker?` | `boolean` | Closes the color picker popover while using the EyeDropper. After the color is selected via the EyeDropper, the color picker popup opens again. Default: `false`. |
280+
| `disableAlphaChannel?` | `boolean` | Disables the transparency slider in the "custom" section of the color picker. Default: `false`. |
281+
282+
#### Return Value
283+
284+
Returns a void `Promise`.
285+
286+
#### Example Usage
287+
288+
```js
289+
const colorPickerButton = document.getElementById("color-picker-button");
290+
291+
colorPickerButton.addEventListener("click", () => {
292+
addOnUISdk.app.showColorPicker(colorPickerButton, {
293+
title: "Add-on's Color Picker",
294+
placement: ColorPickerPlacement.left,
295+
eyedropperHidesPicker: true,
296+
disableAlphaChannel: false,
297+
});
298+
});
299+
300+
colorPickerButton.addEventListener(ColorPickerEvents.colorChange, (event) => {
301+
console.log("Color change event received!", event.detail.color;);
302+
});
303+
```
304+
305+
### hideColorPicker()
306+
307+
Hides the Adobe Express color picker.
308+
309+
#### Signature
310+
311+
`hideColorPicker(): Promise<void>;`
312+
313+
#### Return Value
314+
315+
Returns a void `Promise`.
316+
317+
#### Example Usage
318+
319+
```js
320+
const colorPickerButton = document.getElementById("color-picker-button");
321+
322+
colorPickerButton.addEventListener("click", () => {
323+
addOnUISdk.app.showColorPicker(colorPickerButton, {
324+
title: "Add-on's Color Picker",
325+
placement: ColorPickerPlacement.left,
326+
eyedropperHidesPicker: true,
327+
disableAlphaChannel: false,
328+
});
329+
setTimeout(() => {
330+
console.log("Hiding Color Picker after 10 seconds...");
331+
addOnUISdk.app.hideColorPicker();
332+
}, 10000);
333+
});
334+
```
335+
257336
### registerIframe()
258337

259338
Allows an iframe hosted within an add-on to register its intent to communicate with the add-on SDK. While iframes can be used for embedding media without SDK interaction, `registerIframe()` is needed for those requiring SDK capabilities. It marks a transition to a more controlled approach, where add-ons must explicitly opt-in to this level of integration.

src/pages/references/addonsdk/addonsdk-constants.md

+24
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,30 @@ A set of constants used throughout the add-on SDK. These constants are equal to
3939
</ul>
4040
</td>
4141
</tr>
42+
<tr class="spectrum-Table-row">
43+
<td class="spectrum-Table-cell"><p><pre>ColorPickerEvents</pre></p></td>
44+
<td class="spectrum-Table-cell"><p><pre>string</pre></p></td>
45+
<td style="vertical-align: bottom;">
46+
<p>Custom events dispatched by the Color Picker.</p>
47+
<ul>
48+
<li><strong>colorChange</strong></li><pre>"colorpicker-color-change"</pre>
49+
<li><strong>close</strong></li><pre>"colorpicker-close"</pre>
50+
</ul>
51+
</td>
52+
</tr>
53+
<tr class="spectrum-Table-row">
54+
<td class="spectrum-Table-cell"><p><pre>ColorPickerPlacement</pre></p></td>
55+
<td class="spectrum-Table-cell"><p><pre>string</pre></p></td>
56+
<td style="vertical-align: bottom;">
57+
<p>Placement of the color picker popover with respect to the anchor element.</p>
58+
<ul>
59+
<li><strong>top</strong></li><pre>"top"</pre>
60+
<li><strong>bottom</strong></li><pre>"bottom"</pre>
61+
<li><strong>left</strong></li><pre>"left"</pre>
62+
<li><strong>right</strong></li><pre>"right"</pre>
63+
</ul>
64+
</td>
65+
</tr>
4266
<tr class="spectrum-Table-row">
4367
<td class="spectrum-Table-cell"><p><pre>DialogResultType</pre></p></td>
4468
<td class="spectrum-Table-cell"><p><pre>string</pre></p></td>

src/pages/references/changelog.md

+9
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ contributors:
2222

2323
# Changelog
2424

25+
## 2025-03-21
26+
27+
### Added
28+
29+
- A native Color Picker is available to add-ons via the [`showColorPicker()`](../references/addonsdk/addonsdk-app.md#showcolorpicker) and [`hideColorPicker()`](../references/addonsdk/addonsdk-app.md#hidecolorpicker) methods of the `addOnUiSdk.app` object.
30+
- We've updated the [Use Color](../guides/develop/how_to/use_color.md) How-to guide, now including a few examples on the Color Picker.
31+
- A [new section](../references/ui-components/color-picker.md) has been added to the documentation, which provides a reference for the Adobe Express built-in UI components available to add-ons, like the Color Picker.
32+
- A new version of the `@adobe/ccweb-add-on-sdk-types` package (v1.14.0) has been released for the CLI. Run `npm update` from the root of your add-on project to update to get the latest typings.
33+
2534
## 2025-03-07
2635

2736
### Added

0 commit comments

Comments
 (0)