Skip to content

Commit

Permalink
Merge branch 'add-shadow-root-support'
Browse files Browse the repository at this point in the history
Choices-js#938

# Conflicts:
#	src/scripts/constants.ts
  • Loading branch information
Xon committed Jul 31, 2024
2 parents 588f13e + a00350d commit dcdc683
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,23 @@ Pass an array of objects:

**Usage:** The maximum amount of search results to show.

### shadowRoot

**Type:** Document Element **Default:** null

**Input types affected:** `select-one`, `select-multiple`

**Usage:** You can pass along the shadowRoot from your application like so.

```js
var shadowRoot = document
.getElementById('wrapper')
.attachShadow({ mode: 'open' });
var choices = new Choices({
shadowRoot: shadowRoot,
});
```

### position

**Type:** `String` **Default:** `auto`
Expand Down
8 changes: 6 additions & 2 deletions src/scripts/choices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ class Choices implements ChoicesInterface {
Choices.defaults.options,
userConfig,
);
// Restore the shadowRoot if provided. deeperge converts it into an empty object.
if (userConfig.shadowRoot) {
this.config.shadowRoot = userConfig.shadowRoot;
}

const invalidConfigOptions = diff(this.config, DEFAULT_CONFIG);
if (invalidConfigOptions.length) {
Expand Down Expand Up @@ -1538,7 +1542,7 @@ class Choices implements ChoicesInterface {
}

_addEventListeners(): void {
const { documentElement } = document;
const documentElement = this.config.shadowRoot || document.documentElement;

// capture events - can cancel event processing or propagation
documentElement.addEventListener('touchend', this._onTouchEnd, true);
Expand Down Expand Up @@ -1592,7 +1596,7 @@ class Choices implements ChoicesInterface {
}

_removeEventListeners(): void {
const { documentElement } = document;
const documentElement = this.config.shadowRoot || document.documentElement;

documentElement.removeEventListener('touchend', this._onTouchEnd, true);
this.containerOuter.element.removeEventListener(
Expand Down
1 change: 1 addition & 0 deletions src/scripts/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export const DEFAULT_CONFIG: Options = {
shouldSort: true,
shouldSortItems: false,
sorter: sortByAlpha,
shadowRoot: null,
placeholder: true,
placeholderValue: null,
searchPlaceholderValue: null,
Expand Down
5 changes: 5 additions & 0 deletions src/scripts/interfaces/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,11 @@ export interface Options {
*/
resetScrollPosition: boolean;

/**
* The shadow root for use within ShadowDom
*/
shadowRoot: any;

/**
* Whether choices and groups should be sorted. If false, choices/groups will appear in the order they were given.
*
Expand Down

0 comments on commit dcdc683

Please sign in to comment.