@spectrum-web-components/reactive-controllers 0.3.3 → 0.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -1
- package/package.json +6 -2
- package/src/Color.dev.js.map +2 -2
- package/src/Color.js.map +2 -2
- package/src/ElementResolution.d.ts +20 -0
- package/src/ElementResolution.dev.js +81 -0
- package/src/ElementResolution.dev.js.map +7 -0
- package/src/ElementResolution.js +2 -0
- package/src/ElementResolution.js.map +7 -0
- package/src/FocusGroup.dev.js.map +2 -2
- package/src/FocusGroup.js.map +2 -2
- package/test/element-resolution.test.js +40 -0
- package/test/element-resolution.test.js.map +7 -0
package/README.md
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
## Description
|
|
2
2
|
|
|
3
|
-
[Reactive controllers](https://lit.dev/docs/composition/controllers/) are a tool for code reuse and composition within [Lit](https://lit.dev), a core dependency of Spectrum Web Components. Reactive controllers can be
|
|
3
|
+
[Reactive controllers](https://lit.dev/docs/composition/controllers/) are a tool for code reuse and composition within [Lit](https://lit.dev), a core dependency of Spectrum Web Components. Reactive controllers can be reused across components to reduce both code complexity and size, and to deliver a consistent user experience. These reactive controllers are used by the Spectrum Web Components library and are published to NPM for you to leverage in your projects as well.
|
|
4
4
|
|
|
5
5
|
### Reactive controllers
|
|
6
6
|
|
|
7
|
+
- [ElementResolutionController](../element-resolution)
|
|
8
|
+
- ColorController
|
|
9
|
+
- FocusGroupController
|
|
10
|
+
- LanguageReslutionController
|
|
7
11
|
- [MatchMediaController](../match-media)
|
|
8
12
|
- [RovingTabindexController](../roving-tab-index)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@spectrum-web-components/reactive-controllers",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.4",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -29,6 +29,10 @@
|
|
|
29
29
|
"development": "./src/Color.dev.js",
|
|
30
30
|
"default": "./src/Color.js"
|
|
31
31
|
},
|
|
32
|
+
"./src/ElementResolution.js": {
|
|
33
|
+
"development": "./src/ElementResolution.dev.js",
|
|
34
|
+
"default": "./src/ElementResolution.js"
|
|
35
|
+
},
|
|
32
36
|
"./src/FocusGroup.js": {
|
|
33
37
|
"development": "./src/FocusGroup.dev.js",
|
|
34
38
|
"default": "./src/FocusGroup.js"
|
|
@@ -75,5 +79,5 @@
|
|
|
75
79
|
"sideEffects": [
|
|
76
80
|
"./**/*.dev.js"
|
|
77
81
|
],
|
|
78
|
-
"gitHead": "
|
|
82
|
+
"gitHead": "370295c7ce89dd64d3d9a4373fd84e861904ec5a"
|
|
79
83
|
}
|
package/src/Color.dev.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["Color.ts"],
|
|
4
|
-
"sourcesContent": ["/*\nCopyright 2022 Adobe. All rights reserved.\nThis file is licensed to you under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License. You may obtain a copy\nof the License at http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under\nthe License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\nOF ANY KIND, either express or implied. See the License for the specific language\ngoverning permissions and limitations under the License.\n*/\n\nimport type { ReactiveElement } from 'lit';\nimport { HSL, HSLA, HSV, HSVA, RGB, RGBA, TinyColor } from '@ctrl/tinycolor';\nexport type { HSL, HSLA, HSV, HSVA, RGB, RGBA, TinyColor };\n\nexport type ColorValue =\n | string\n | number\n | TinyColor\n | HSVA\n | HSV\n | RGB\n | RGBA\n | HSL\n | HSLA;\n\nexport const extractHueAndSaturationRegExp =\n /^hs[v|l]a?\\s?\\((\\d{1,3}\\.?\\d*?),?\\s?(\\d{1,3})/;\nexport const replaceHueAndSaturationRegExp =\n /(^hs[v|l]a?\\s?\\()\\d{1,3}\\.?\\d*?(,?\\s?)\\d{1,3}/;\nexport const replaceHueRegExp = /(^hs[v|l]a?\\()\\d{1,3}/;\n\ntype TinyColorToValue = {\n toHex: ColorValue;\n toHexString: ColorValue;\n toHsv: ColorValue;\n toHsvString: ColorValue;\n toHsl: ColorValue;\n toHslString: ColorValue;\n toHex8: ColorValue;\n toHex8String: ColorValue;\n toPercentageRgb: ColorValue;\n toPercentageRgbString: ColorValue;\n toRgb: ColorValue;\n toRgbString: ColorValue;\n};\n\nconst getHexValue = (color: TinyColor, isString: boolean): ColorValue =>\n isString ? color.toHexString() : color.toHex();\n\nexport class ColorController {\n protected host: ReactiveElement;\n\n protected applyColorToState!: ({\n h,\n s,\n v,\n }: {\n h: number;\n s: number;\n v: number;\n }) => void;\n\n protected extractColorFromState!: (\n controller: ColorController\n ) => ColorValue;\n\n protected setColorProcess(\n currentColor: TinyColor,\n nextColor: ColorValue,\n format: string,\n isString: boolean\n ): void {\n if (this.maintains === 'hue') {\n this.setColorMaintainHue(currentColor, nextColor, format, isString);\n } else if (this.maintains === 'saturation') {\n this.setColorMaintainSaturation(\n currentColor,\n nextColor,\n format,\n isString\n );\n }\n }\n\n protected setColorMaintainHue(\n currentColor: TinyColor,\n nextColor: ColorValue,\n format: string,\n isString: boolean\n ): void {\n const { h, s, v } = this._color.toHsv();\n let originalHue: number | undefined = undefined;\n\n if (isString && format.startsWith('hs')) {\n const values = extractHueAndSaturationRegExp.exec(\n nextColor as string\n );\n\n if (values !== null) {\n const [, h] = values;\n originalHue = Number(h);\n }\n } else if (!isString && format.startsWith('hs')) {\n const colorInput = currentColor.originalInput;\n const colorValues = Object.values(colorInput);\n originalHue = colorValues[0];\n }\n\n this.hue = originalHue || h;\n this.applyColorToState({ h, s, v });\n }\n\n protected setColorMaintainSaturation(\n currentColor: TinyColor,\n nextColor: ColorValue,\n format: string,\n isString: boolean\n ): void {\n if (isString && format.startsWith('hs')) {\n const values = extractHueAndSaturationRegExp.exec(\n nextColor as string\n );\n\n if (values !== null) {\n const [, h, s] = values;\n this.hue = Number(h);\n this.saturation = Number(s);\n }\n } else if (!isString && format.startsWith('hs')) {\n const colorInput = currentColor.originalInput;\n const colorValues = Object.values(colorInput);\n this.hue = colorValues[0];\n this.saturation = colorValues[1];\n } else {\n const { h } = currentColor.toHsv();\n this.hue = h;\n }\n this.applyColorToState(currentColor.toHsv());\n }\n\n protected maintains: 'hue' | 'saturation' = 'hue';\n private saturation!: number;\n\n constructor(\n host: ReactiveElement,\n {\n applyColorToState,\n extractColorFromState,\n maintains,\n }: {\n applyColorToState({\n h,\n s,\n v,\n }: {\n h: number;\n s: number;\n v: number;\n }): void;\n extractColorFromState(controller: ColorController): ColorValue;\n maintains?: 'hue' | 'saturation';\n }\n ) {\n this.host = host;\n this.applyColorToState = applyColorToState;\n this.extractColorFromState = extractColorFromState;\n this.maintains = maintains || this.maintains;\n }\n\n public applyColorFromState(): void {\n this._color = new TinyColor(this.extractColorFromState(this));\n }\n\n public get hue(): number {\n return this._hue;\n }\n\n public set hue(value: number) {\n const hue = Math.min(360, Math.max(0, value));\n if (hue === this.hue) {\n return;\n }\n const oldValue = this.hue;\n const { s, v } = this._color.toHsv();\n this._color = new TinyColor({ h: hue, s, v });\n this._hue = hue;\n this.host.requestUpdate('hue', oldValue);\n }\n\n private _hue = 0;\n\n protected getColorProcesses: Record<\n string,\n (color: TinyColor, isString: boolean) => ColorValue\n > = {\n rgb: (color, isString) =>\n isString ? color.toRgbString() : color.toRgb(),\n prgb: (color, isString) =>\n isString ? color.toPercentageRgbString() : color.toPercentageRgb(),\n hex8: (color, isString) =>\n isString ? color.toHex8String() : color.toHex8(),\n name: (color) => color.toName() || color.toRgbString(),\n hsl: (color, isString) => {\n if (this.maintains === 'hue') {\n if (isString) {\n const hslString = color.toHslString();\n return hslString.replace(replaceHueRegExp, `$1${this.hue}`);\n } else {\n const { s, l, a } = color.toHsl();\n return { h: this.hue, s, l, a };\n }\n } else {\n if (isString) {\n const hslString = color.toHslString();\n return hslString.replace(\n replaceHueAndSaturationRegExp,\n `$1${this.hue}$2${this.saturation}`\n );\n } else {\n const { s, l, a } = color.toHsl();\n return { h: this.hue, s, l, a };\n }\n }\n },\n hsv: (color, isString) => {\n if (this.maintains === 'hue') {\n if (isString) {\n const hsvString = color.toHsvString();\n return hsvString.replace(replaceHueRegExp, `$1${this.hue}`);\n } else {\n const { s, v, a } = color.toHsv();\n return { h: this.hue, s, v, a };\n }\n } else {\n if (isString) {\n const hsvString = color.toHsvString();\n return hsvString.replace(\n replaceHueAndSaturationRegExp,\n `$1${this.hue}$2${this.saturation}`\n );\n } else {\n const { s, v, a } = color.toHsv();\n return { h: this.hue, s, v, a };\n }\n }\n },\n hex: getHexValue,\n hex3: getHexValue,\n hex4: getHexValue,\n hex6: getHexValue,\n };\n\n public get value(): ColorValue {\n return this.color;\n }\n\n public get color(): ColorValue {\n return this.getColorProcesses[this._format.format || 'hex'](\n this._color,\n this._format.isString\n );\n }\n\n public set color(color: ColorValue) {\n if (color === this.color) {\n return;\n }\n const oldValue = this._color;\n this._color = new TinyColor(color);\n const format = this._color.format;\n let isString = typeof color === 'string' || color instanceof String;\n\n if (format.startsWith('hex')) {\n isString = (color as string).startsWith('#');\n }\n\n this._format = {\n format,\n isString,\n };\n\n this.setColorProcess(this._color, color, format, isString);\n this.host.requestUpdate('color', oldValue);\n }\n\n private _color = new TinyColor({ h: 0, s: 1, v: 1 });\n\n public getColor(format: string): ColorValue {\n const formatOptions: Record<string, keyof TinyColorToValue> = {\n hsl: 'toHsl',\n };\n return this._color[formatOptions[format]]();\n }\n\n public setColor(color: TinyColor): void {\n this._color = color;\n const isString =\n typeof this._color.originalInput === 'string' ||\n this._color.originalInput instanceof String;\n this.setColorProcess(this._color, color, this._color.format, isString);\n }\n\n public getHslString(): string {\n return this._color.toHslString();\n }\n\n private _previousColor = new TinyColor({ h: 0, s: 1, v: 1 });\n\n public savePreviousColor(): void {\n this._previousColor = this._color.clone();\n }\n\n public restorePreviousColor(): void {\n this.setColor(this._previousColor);\n }\n\n private _format: { format: string; isString: boolean } = {\n format: '',\n isString: false,\n };\n}\n"],
|
|
5
|
-
"mappings": ";AAaA,SAA0C,iBAAiB;AAcpD,aAAM,gCACT;AACG,aAAM,gCACT;AACG,aAAM,mBAAmB;AAiBhC,MAAM,cAAc,CAAC,OAAkB,aACnC,WAAW,MAAM,YAAY,IAAI,MAAM,MAAM;AAE1C,aAAM,gBAAgB;AAAA,EA8FzB,YACI,MACA;AAAA,IACI;AAAA,IACA;AAAA,IACA;AAAA,EACJ,GAaF;AAtBF,SAAU,YAAkC;AAiD5C,SAAQ,OAAO;AAEf,SAAU,oBAGN;AAAA,MACA,KAAK,CAAC,OAAO,aACT,WAAW,MAAM,YAAY,IAAI,MAAM,MAAM;AAAA,MACjD,MAAM,CAAC,OAAO,aACV,WAAW,MAAM,sBAAsB,IAAI,MAAM,gBAAgB;AAAA,MACrE,MAAM,CAAC,OAAO,aACV,WAAW,MAAM,aAAa,IAAI,MAAM,OAAO;AAAA,MACnD,MAAM,CAAC,UAAU,MAAM,OAAO,KAAK,MAAM,YAAY;AAAA,MACrD,KAAK,CAAC,OAAO,aAAa;AACtB,YAAI,KAAK,cAAc,OAAO;AAC1B,cAAI,UAAU;AACV,kBAAM,YAAY,MAAM,YAAY;AACpC,mBAAO,UAAU,QAAQ,kBAAkB,KAAK,KAAK,KAAK;AAAA,UAC9D,OAAO;AACH,kBAAM,EAAE,GAAG,GAAG,EAAE,IAAI,MAAM,MAAM;AAChC,mBAAO,EAAE,GAAG,KAAK,KAAK,GAAG,GAAG,EAAE;AAAA,UAClC;AAAA,QACJ,OAAO;AACH,cAAI,UAAU;AACV,kBAAM,YAAY,MAAM,YAAY;AACpC,mBAAO,UAAU;AAAA,cACb;AAAA,cACA,KAAK,KAAK,QAAQ,KAAK;AAAA,YAC3B;AAAA,UACJ,OAAO;AACH,kBAAM,EAAE,GAAG,GAAG,EAAE,IAAI,MAAM,MAAM;AAChC,mBAAO,EAAE,GAAG,KAAK,KAAK,GAAG,GAAG,EAAE;AAAA,UAClC;AAAA,QACJ;AAAA,MACJ;AAAA,MACA,KAAK,CAAC,OAAO,aAAa;AACtB,YAAI,KAAK,cAAc,OAAO;AAC1B,cAAI,UAAU;AACV,kBAAM,YAAY,MAAM,YAAY;AACpC,mBAAO,UAAU,QAAQ,kBAAkB,KAAK,KAAK,KAAK;AAAA,UAC9D,OAAO;AACH,kBAAM,EAAE,GAAG,GAAG,EAAE,IAAI,MAAM,MAAM;AAChC,mBAAO,EAAE,GAAG,KAAK,KAAK,GAAG,GAAG,EAAE;AAAA,UAClC;AAAA,QACJ,OAAO;AACH,cAAI,UAAU;AACV,kBAAM,YAAY,MAAM,YAAY;AACpC,mBAAO,UAAU;AAAA,cACb;AAAA,cACA,KAAK,KAAK,QAAQ,KAAK;AAAA,YAC3B;AAAA,UACJ,OAAO;AACH,kBAAM,EAAE,GAAG,GAAG,EAAE,IAAI,MAAM,MAAM;AAChC,mBAAO,EAAE,GAAG,KAAK,KAAK,GAAG,GAAG,EAAE;AAAA,UAClC;AAAA,QACJ;AAAA,MACJ;AAAA,MACA,KAAK;AAAA,MACL,MAAM;AAAA,MACN,MAAM;AAAA,MACN,MAAM;AAAA,IACV;
|
|
4
|
+
"sourcesContent": ["/*\nCopyright 2022 Adobe. All rights reserved.\nThis file is licensed to you under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License. You may obtain a copy\nof the License at http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under\nthe License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\nOF ANY KIND, either express or implied. See the License for the specific language\ngoverning permissions and limitations under the License.\n*/\n\nimport type { ReactiveElement } from 'lit';\nimport { HSL, HSLA, HSV, HSVA, RGB, RGBA, TinyColor } from '@ctrl/tinycolor';\nexport type { HSL, HSLA, HSV, HSVA, RGB, RGBA, TinyColor };\n\nexport type ColorValue =\n | string\n | number\n | TinyColor\n | HSVA\n | HSV\n | RGB\n | RGBA\n | HSL\n | HSLA;\n\nexport const extractHueAndSaturationRegExp =\n /^hs[v|l]a?\\s?\\((\\d{1,3}\\.?\\d*?),?\\s?(\\d{1,3})/;\nexport const replaceHueAndSaturationRegExp =\n /(^hs[v|l]a?\\s?\\()\\d{1,3}\\.?\\d*?(,?\\s?)\\d{1,3}/;\nexport const replaceHueRegExp = /(^hs[v|l]a?\\()\\d{1,3}/;\n\ntype TinyColorToValue = {\n toHex: ColorValue;\n toHexString: ColorValue;\n toHsv: ColorValue;\n toHsvString: ColorValue;\n toHsl: ColorValue;\n toHslString: ColorValue;\n toHex8: ColorValue;\n toHex8String: ColorValue;\n toPercentageRgb: ColorValue;\n toPercentageRgbString: ColorValue;\n toRgb: ColorValue;\n toRgbString: ColorValue;\n};\n\nconst getHexValue = (color: TinyColor, isString: boolean): ColorValue =>\n isString ? color.toHexString() : color.toHex();\n\nexport class ColorController {\n protected host: ReactiveElement;\n\n protected applyColorToState!: ({\n h,\n s,\n v,\n }: {\n h: number;\n s: number;\n v: number;\n }) => void;\n\n protected extractColorFromState!: (\n controller: ColorController\n ) => ColorValue;\n\n protected setColorProcess(\n currentColor: TinyColor,\n nextColor: ColorValue,\n format: string,\n isString: boolean\n ): void {\n if (this.maintains === 'hue') {\n this.setColorMaintainHue(currentColor, nextColor, format, isString);\n } else if (this.maintains === 'saturation') {\n this.setColorMaintainSaturation(\n currentColor,\n nextColor,\n format,\n isString\n );\n }\n }\n\n protected setColorMaintainHue(\n currentColor: TinyColor,\n nextColor: ColorValue,\n format: string,\n isString: boolean\n ): void {\n const { h, s, v } = this._color.toHsv();\n let originalHue: number | undefined = undefined;\n\n if (isString && format.startsWith('hs')) {\n const values = extractHueAndSaturationRegExp.exec(\n nextColor as string\n );\n\n if (values !== null) {\n const [, h] = values;\n originalHue = Number(h);\n }\n } else if (!isString && format.startsWith('hs')) {\n const colorInput = currentColor.originalInput;\n const colorValues = Object.values(colorInput);\n originalHue = colorValues[0];\n }\n\n this.hue = originalHue || h;\n this.applyColorToState({ h, s, v });\n }\n\n protected setColorMaintainSaturation(\n currentColor: TinyColor,\n nextColor: ColorValue,\n format: string,\n isString: boolean\n ): void {\n if (isString && format.startsWith('hs')) {\n const values = extractHueAndSaturationRegExp.exec(\n nextColor as string\n );\n\n if (values !== null) {\n const [, h, s] = values;\n this.hue = Number(h);\n this.saturation = Number(s);\n }\n } else if (!isString && format.startsWith('hs')) {\n const colorInput = currentColor.originalInput;\n const colorValues = Object.values(colorInput);\n this.hue = colorValues[0];\n this.saturation = colorValues[1];\n } else {\n const { h } = currentColor.toHsv();\n this.hue = h;\n }\n this.applyColorToState(currentColor.toHsv());\n }\n\n protected maintains: 'hue' | 'saturation' = 'hue';\n private saturation!: number;\n\n constructor(\n host: ReactiveElement,\n {\n applyColorToState,\n extractColorFromState,\n maintains,\n }: {\n applyColorToState({\n h,\n s,\n v,\n }: {\n h: number;\n s: number;\n v: number;\n }): void;\n extractColorFromState(controller: ColorController): ColorValue;\n maintains?: 'hue' | 'saturation';\n }\n ) {\n this.host = host;\n this.applyColorToState = applyColorToState;\n this.extractColorFromState = extractColorFromState;\n this.maintains = maintains || this.maintains;\n }\n\n public applyColorFromState(): void {\n this._color = new TinyColor(this.extractColorFromState(this));\n }\n\n public get hue(): number {\n return this._hue;\n }\n\n public set hue(value: number) {\n const hue = Math.min(360, Math.max(0, value));\n if (hue === this.hue) {\n return;\n }\n const oldValue = this.hue;\n const { s, v } = this._color.toHsv();\n this._color = new TinyColor({ h: hue, s, v });\n this._hue = hue;\n this.host.requestUpdate('hue', oldValue);\n }\n\n private _hue = 0;\n\n protected getColorProcesses: Record<\n string,\n (color: TinyColor, isString: boolean) => ColorValue\n > = {\n rgb: (color, isString) =>\n isString ? color.toRgbString() : color.toRgb(),\n prgb: (color, isString) =>\n isString ? color.toPercentageRgbString() : color.toPercentageRgb(),\n hex8: (color, isString) =>\n isString ? color.toHex8String() : color.toHex8(),\n name: (color) => color.toName() || color.toRgbString(),\n hsl: (color, isString) => {\n if (this.maintains === 'hue') {\n if (isString) {\n const hslString = color.toHslString();\n return hslString.replace(replaceHueRegExp, `$1${this.hue}`);\n } else {\n const { s, l, a } = color.toHsl();\n return { h: this.hue, s, l, a };\n }\n } else {\n if (isString) {\n const hslString = color.toHslString();\n return hslString.replace(\n replaceHueAndSaturationRegExp,\n `$1${this.hue}$2${this.saturation}`\n );\n } else {\n const { s, l, a } = color.toHsl();\n return { h: this.hue, s, l, a };\n }\n }\n },\n hsv: (color, isString) => {\n if (this.maintains === 'hue') {\n if (isString) {\n const hsvString = color.toHsvString();\n return hsvString.replace(replaceHueRegExp, `$1${this.hue}`);\n } else {\n const { s, v, a } = color.toHsv();\n return { h: this.hue, s, v, a };\n }\n } else {\n if (isString) {\n const hsvString = color.toHsvString();\n return hsvString.replace(\n replaceHueAndSaturationRegExp,\n `$1${this.hue}$2${this.saturation}`\n );\n } else {\n const { s, v, a } = color.toHsv();\n return { h: this.hue, s, v, a };\n }\n }\n },\n hex: getHexValue,\n hex3: getHexValue,\n hex4: getHexValue,\n hex6: getHexValue,\n };\n\n /* c8 ignore next 3 */\n public get value(): ColorValue {\n return this.color;\n }\n\n public get color(): ColorValue {\n return this.getColorProcesses[this._format.format || 'hex'](\n this._color,\n this._format.isString\n );\n }\n\n public set color(color: ColorValue) {\n /* c8 ignore next 3 */\n if (color === this.color) {\n return;\n }\n const oldValue = this._color;\n this._color = new TinyColor(color);\n const format = this._color.format;\n let isString = typeof color === 'string' || color instanceof String;\n\n if (format.startsWith('hex')) {\n isString = (color as string).startsWith('#');\n }\n\n this._format = {\n format,\n isString,\n };\n\n this.setColorProcess(this._color, color, format, isString);\n this.host.requestUpdate('color', oldValue);\n }\n\n private _color = new TinyColor({ h: 0, s: 1, v: 1 });\n\n public getColor(format: string): ColorValue {\n const formatOptions: Record<string, keyof TinyColorToValue> = {\n hsl: 'toHsl',\n };\n return this._color[formatOptions[format]]();\n }\n\n public setColor(color: TinyColor): void {\n this._color = color;\n const isString =\n typeof this._color.originalInput === 'string' ||\n this._color.originalInput instanceof String;\n this.setColorProcess(this._color, color, this._color.format, isString);\n }\n\n public getHslString(): string {\n return this._color.toHslString();\n }\n\n private _previousColor = new TinyColor({ h: 0, s: 1, v: 1 });\n\n public savePreviousColor(): void {\n this._previousColor = this._color.clone();\n }\n\n public restorePreviousColor(): void {\n this.setColor(this._previousColor);\n }\n\n private _format: { format: string; isString: boolean } = {\n format: '',\n isString: false,\n };\n}\n"],
|
|
5
|
+
"mappings": ";AAaA,SAA0C,iBAAiB;AAcpD,aAAM,gCACT;AACG,aAAM,gCACT;AACG,aAAM,mBAAmB;AAiBhC,MAAM,cAAc,CAAC,OAAkB,aACnC,WAAW,MAAM,YAAY,IAAI,MAAM,MAAM;AAE1C,aAAM,gBAAgB;AAAA,EA8FzB,YACI,MACA;AAAA,IACI;AAAA,IACA;AAAA,IACA;AAAA,EACJ,GAaF;AAtBF,SAAU,YAAkC;AAiD5C,SAAQ,OAAO;AAEf,SAAU,oBAGN;AAAA,MACA,KAAK,CAAC,OAAO,aACT,WAAW,MAAM,YAAY,IAAI,MAAM,MAAM;AAAA,MACjD,MAAM,CAAC,OAAO,aACV,WAAW,MAAM,sBAAsB,IAAI,MAAM,gBAAgB;AAAA,MACrE,MAAM,CAAC,OAAO,aACV,WAAW,MAAM,aAAa,IAAI,MAAM,OAAO;AAAA,MACnD,MAAM,CAAC,UAAU,MAAM,OAAO,KAAK,MAAM,YAAY;AAAA,MACrD,KAAK,CAAC,OAAO,aAAa;AACtB,YAAI,KAAK,cAAc,OAAO;AAC1B,cAAI,UAAU;AACV,kBAAM,YAAY,MAAM,YAAY;AACpC,mBAAO,UAAU,QAAQ,kBAAkB,KAAK,KAAK,KAAK;AAAA,UAC9D,OAAO;AACH,kBAAM,EAAE,GAAG,GAAG,EAAE,IAAI,MAAM,MAAM;AAChC,mBAAO,EAAE,GAAG,KAAK,KAAK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iBAAY,MAAiB,WAAW,GAAG;AAAA,IAC/C;AAEA,SAAK,UAAU;AAAA,MACX;AAAA,MACA;AAAA,IACJ;AAEA,SAAK,gBAAgB,KAAK,QAAQ,OAAO,QAAQ,QAAQ;AACzD,SAAK,KAAK,cAAc,SAAS,QAAQ;AAAA,EAC7C;AAAA,EAIO,SAAS,QAA4B;AACxC,UAAM,gBAAwD;AAAA,MAC1D,KAAK;AAAA,IACT;AACA,WAAO,KAAK,OAAO,cAAc,SAAS;AAAA,EAC9C;AAAA,EAEO,SAAS,OAAwB;AACpC,SAAK,SAAS;AACd,UAAM,WACF,OAAO,KAAK,OAAO,kBAAkB,YACrC,KAAK,OAAO,yBAAyB;AACzC,SAAK,gBAAgB,KAAK,QAAQ,OAAO,KAAK,OAAO,QAAQ,QAAQ;AAAA,EACzE;AAAA,EAEO,eAAuB;AAC1B,WAAO,KAAK,OAAO,YAAY;AAAA,EACnC;AAAA,EAIO,oBAA0B;AAC7B,SAAK,iBAAiB,KAAK,OAAO,MAAM;AAAA,EAC5C;AAAA,EAEO,uBAA6B;AAChC,SAAK,SAAS,KAAK,cAAc;AAAA,EACrC;AAMJ;",
|
|
6
6
|
"names": ["h"]
|
|
7
7
|
}
|
package/src/Color.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["Color.ts"],
|
|
4
|
-
"sourcesContent": ["/*\nCopyright 2022 Adobe. All rights reserved.\nThis file is licensed to you under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License. You may obtain a copy\nof the License at http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under\nthe License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\nOF ANY KIND, either express or implied. See the License for the specific language\ngoverning permissions and limitations under the License.\n*/\n\nimport type { ReactiveElement } from 'lit';\nimport { HSL, HSLA, HSV, HSVA, RGB, RGBA, TinyColor } from '@ctrl/tinycolor';\nexport type { HSL, HSLA, HSV, HSVA, RGB, RGBA, TinyColor };\n\nexport type ColorValue =\n | string\n | number\n | TinyColor\n | HSVA\n | HSV\n | RGB\n | RGBA\n | HSL\n | HSLA;\n\nexport const extractHueAndSaturationRegExp =\n /^hs[v|l]a?\\s?\\((\\d{1,3}\\.?\\d*?),?\\s?(\\d{1,3})/;\nexport const replaceHueAndSaturationRegExp =\n /(^hs[v|l]a?\\s?\\()\\d{1,3}\\.?\\d*?(,?\\s?)\\d{1,3}/;\nexport const replaceHueRegExp = /(^hs[v|l]a?\\()\\d{1,3}/;\n\ntype TinyColorToValue = {\n toHex: ColorValue;\n toHexString: ColorValue;\n toHsv: ColorValue;\n toHsvString: ColorValue;\n toHsl: ColorValue;\n toHslString: ColorValue;\n toHex8: ColorValue;\n toHex8String: ColorValue;\n toPercentageRgb: ColorValue;\n toPercentageRgbString: ColorValue;\n toRgb: ColorValue;\n toRgbString: ColorValue;\n};\n\nconst getHexValue = (color: TinyColor, isString: boolean): ColorValue =>\n isString ? color.toHexString() : color.toHex();\n\nexport class ColorController {\n protected host: ReactiveElement;\n\n protected applyColorToState!: ({\n h,\n s,\n v,\n }: {\n h: number;\n s: number;\n v: number;\n }) => void;\n\n protected extractColorFromState!: (\n controller: ColorController\n ) => ColorValue;\n\n protected setColorProcess(\n currentColor: TinyColor,\n nextColor: ColorValue,\n format: string,\n isString: boolean\n ): void {\n if (this.maintains === 'hue') {\n this.setColorMaintainHue(currentColor, nextColor, format, isString);\n } else if (this.maintains === 'saturation') {\n this.setColorMaintainSaturation(\n currentColor,\n nextColor,\n format,\n isString\n );\n }\n }\n\n protected setColorMaintainHue(\n currentColor: TinyColor,\n nextColor: ColorValue,\n format: string,\n isString: boolean\n ): void {\n const { h, s, v } = this._color.toHsv();\n let originalHue: number | undefined = undefined;\n\n if (isString && format.startsWith('hs')) {\n const values = extractHueAndSaturationRegExp.exec(\n nextColor as string\n );\n\n if (values !== null) {\n const [, h] = values;\n originalHue = Number(h);\n }\n } else if (!isString && format.startsWith('hs')) {\n const colorInput = currentColor.originalInput;\n const colorValues = Object.values(colorInput);\n originalHue = colorValues[0];\n }\n\n this.hue = originalHue || h;\n this.applyColorToState({ h, s, v });\n }\n\n protected setColorMaintainSaturation(\n currentColor: TinyColor,\n nextColor: ColorValue,\n format: string,\n isString: boolean\n ): void {\n if (isString && format.startsWith('hs')) {\n const values = extractHueAndSaturationRegExp.exec(\n nextColor as string\n );\n\n if (values !== null) {\n const [, h, s] = values;\n this.hue = Number(h);\n this.saturation = Number(s);\n }\n } else if (!isString && format.startsWith('hs')) {\n const colorInput = currentColor.originalInput;\n const colorValues = Object.values(colorInput);\n this.hue = colorValues[0];\n this.saturation = colorValues[1];\n } else {\n const { h } = currentColor.toHsv();\n this.hue = h;\n }\n this.applyColorToState(currentColor.toHsv());\n }\n\n protected maintains: 'hue' | 'saturation' = 'hue';\n private saturation!: number;\n\n constructor(\n host: ReactiveElement,\n {\n applyColorToState,\n extractColorFromState,\n maintains,\n }: {\n applyColorToState({\n h,\n s,\n v,\n }: {\n h: number;\n s: number;\n v: number;\n }): void;\n extractColorFromState(controller: ColorController): ColorValue;\n maintains?: 'hue' | 'saturation';\n }\n ) {\n this.host = host;\n this.applyColorToState = applyColorToState;\n this.extractColorFromState = extractColorFromState;\n this.maintains = maintains || this.maintains;\n }\n\n public applyColorFromState(): void {\n this._color = new TinyColor(this.extractColorFromState(this));\n }\n\n public get hue(): number {\n return this._hue;\n }\n\n public set hue(value: number) {\n const hue = Math.min(360, Math.max(0, value));\n if (hue === this.hue) {\n return;\n }\n const oldValue = this.hue;\n const { s, v } = this._color.toHsv();\n this._color = new TinyColor({ h: hue, s, v });\n this._hue = hue;\n this.host.requestUpdate('hue', oldValue);\n }\n\n private _hue = 0;\n\n protected getColorProcesses: Record<\n string,\n (color: TinyColor, isString: boolean) => ColorValue\n > = {\n rgb: (color, isString) =>\n isString ? color.toRgbString() : color.toRgb(),\n prgb: (color, isString) =>\n isString ? color.toPercentageRgbString() : color.toPercentageRgb(),\n hex8: (color, isString) =>\n isString ? color.toHex8String() : color.toHex8(),\n name: (color) => color.toName() || color.toRgbString(),\n hsl: (color, isString) => {\n if (this.maintains === 'hue') {\n if (isString) {\n const hslString = color.toHslString();\n return hslString.replace(replaceHueRegExp, `$1${this.hue}`);\n } else {\n const { s, l, a } = color.toHsl();\n return { h: this.hue, s, l, a };\n }\n } else {\n if (isString) {\n const hslString = color.toHslString();\n return hslString.replace(\n replaceHueAndSaturationRegExp,\n `$1${this.hue}$2${this.saturation}`\n );\n } else {\n const { s, l, a } = color.toHsl();\n return { h: this.hue, s, l, a };\n }\n }\n },\n hsv: (color, isString) => {\n if (this.maintains === 'hue') {\n if (isString) {\n const hsvString = color.toHsvString();\n return hsvString.replace(replaceHueRegExp, `$1${this.hue}`);\n } else {\n const { s, v, a } = color.toHsv();\n return { h: this.hue, s, v, a };\n }\n } else {\n if (isString) {\n const hsvString = color.toHsvString();\n return hsvString.replace(\n replaceHueAndSaturationRegExp,\n `$1${this.hue}$2${this.saturation}`\n );\n } else {\n const { s, v, a } = color.toHsv();\n return { h: this.hue, s, v, a };\n }\n }\n },\n hex: getHexValue,\n hex3: getHexValue,\n hex4: getHexValue,\n hex6: getHexValue,\n };\n\n public get value(): ColorValue {\n return this.color;\n }\n\n public get color(): ColorValue {\n return this.getColorProcesses[this._format.format || 'hex'](\n this._color,\n this._format.isString\n );\n }\n\n public set color(color: ColorValue) {\n if (color === this.color) {\n return;\n }\n const oldValue = this._color;\n this._color = new TinyColor(color);\n const format = this._color.format;\n let isString = typeof color === 'string' || color instanceof String;\n\n if (format.startsWith('hex')) {\n isString = (color as string).startsWith('#');\n }\n\n this._format = {\n format,\n isString,\n };\n\n this.setColorProcess(this._color, color, format, isString);\n this.host.requestUpdate('color', oldValue);\n }\n\n private _color = new TinyColor({ h: 0, s: 1, v: 1 });\n\n public getColor(format: string): ColorValue {\n const formatOptions: Record<string, keyof TinyColorToValue> = {\n hsl: 'toHsl',\n };\n return this._color[formatOptions[format]]();\n }\n\n public setColor(color: TinyColor): void {\n this._color = color;\n const isString =\n typeof this._color.originalInput === 'string' ||\n this._color.originalInput instanceof String;\n this.setColorProcess(this._color, color, this._color.format, isString);\n }\n\n public getHslString(): string {\n return this._color.toHslString();\n }\n\n private _previousColor = new TinyColor({ h: 0, s: 1, v: 1 });\n\n public savePreviousColor(): void {\n this._previousColor = this._color.clone();\n }\n\n public restorePreviousColor(): void {\n this.setColor(this._previousColor);\n }\n\n private _format: { format: string; isString: boolean } = {\n format: '',\n isString: false,\n };\n}\n"],
|
|
5
|
-
"mappings": "aAaA,OAA0C,aAAAA,MAAiB,kBAcpD,aAAM,8BACT,gDACS,8BACT,gDACS,iBAAmB,wBAiBhC,MAAMC,EAAc,CAACC,EAAkBC,IACnCA,EAAWD,EAAM,YAAY,EAAIA,EAAM,MAAM,EAE1C,aAAM,eAAgB,CA8FzB,YACIE,EACA,CACI,kBAAAC,EACA,sBAAAC,EACA,UAAAC,CACJ,EAaF,CAtBF,KAAU,UAAkC,MAiD5C,KAAQ,KAAO,EAEf,KAAU,kBAGN,CACA,IAAK,CAACL,EAAOC,IACTA,EAAWD,EAAM,YAAY,EAAIA,EAAM,MAAM,EACjD,KAAM,CAACA,EAAOC,IACVA,EAAWD,EAAM,sBAAsB,EAAIA,EAAM,gBAAgB,EACrE,KAAM,CAACA,EAAOC,IACVA,EAAWD,EAAM,aAAa,EAAIA,EAAM,OAAO,EACnD,KAAOA,GAAUA,EAAM,OAAO,GAAKA,EAAM,YAAY,EACrD,IAAK,CAACA,EAAOC,IAAa,CACtB,GAAI,KAAK,YAAc,MAAO,CAC1B,GAAIA,EAEA,OADkBD,EAAM,YAAY,EACnB,QAAQ,iBAAkB,KAAK,KAAK,KAAK,EACvD,CACH,KAAM,CAAE,EAAAM,EAAG,EAAAC,EAAG,EAAAC,CAAE,EAAIR,EAAM,MAAM,EAChC,MAAO,CAAE,EAAG,KAAK,IAAK,EAAAM,EAAG,EAAAC,EAAG,EAAAC,CAAE,CAClC,CACJ,KAAO,CACH,GAAIP,EAEA,OADkBD,EAAM,YAAY,EACnB,QACb,8BACA,KAAK,KAAK,QAAQ,KAAK,YAC3B,EACG,CACH,KAAM,CAAE,EAAAM,EAAG,EAAAC,EAAG,EAAAC,CAAE,EAAIR,EAAM,MAAM,EAChC,MAAO,CAAE,EAAG,KAAK,IAAK,EAAAM,EAAG,EAAAC,EAAG,EAAAC,CAAE,CAClC,CACJ,CACJ,EACA,IAAK,CAACR,EAAOC,IAAa,CACtB,GAAI,KAAK,YAAc,MAAO,CAC1B,GAAIA,EAEA,OADkBD,EAAM,YAAY,EACnB,QAAQ,iBAAkB,KAAK,KAAK,KAAK,EACvD,CACH,KAAM,CAAE,EAAAM,EAAG,EAAAG,EAAG,EAAAD,CAAE,EAAIR,EAAM,MAAM,EAChC,MAAO,CAAE,EAAG,KAAK,IAAK,EAAAM,EAAG,EAAAG,EAAG,EAAAD,CAAE,CAClC,CACJ,KAAO,CACH,GAAIP,EAEA,OADkBD,EAAM,YAAY,EACnB,QACb,8BACA,KAAK,KAAK,QAAQ,KAAK,YAC3B,EACG,CACH,KAAM,CAAE,EAAAM,EAAG,EAAAG,EAAG,EAAAD,CAAE,EAAIR,EAAM,MAAM,EAChC,MAAO,CAAE,EAAG,KAAK,IAAK,EAAAM,EAAG,EAAAG,EAAG,EAAAD,CAAE,CAClC,CACJ,CACJ,EACA,IAAKT,EACL,KAAMA,EACN,KAAMA,EACN,KAAMA,CACV,
|
|
4
|
+
"sourcesContent": ["/*\nCopyright 2022 Adobe. All rights reserved.\nThis file is licensed to you under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License. You may obtain a copy\nof the License at http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under\nthe License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\nOF ANY KIND, either express or implied. See the License for the specific language\ngoverning permissions and limitations under the License.\n*/\n\nimport type { ReactiveElement } from 'lit';\nimport { HSL, HSLA, HSV, HSVA, RGB, RGBA, TinyColor } from '@ctrl/tinycolor';\nexport type { HSL, HSLA, HSV, HSVA, RGB, RGBA, TinyColor };\n\nexport type ColorValue =\n | string\n | number\n | TinyColor\n | HSVA\n | HSV\n | RGB\n | RGBA\n | HSL\n | HSLA;\n\nexport const extractHueAndSaturationRegExp =\n /^hs[v|l]a?\\s?\\((\\d{1,3}\\.?\\d*?),?\\s?(\\d{1,3})/;\nexport const replaceHueAndSaturationRegExp =\n /(^hs[v|l]a?\\s?\\()\\d{1,3}\\.?\\d*?(,?\\s?)\\d{1,3}/;\nexport const replaceHueRegExp = /(^hs[v|l]a?\\()\\d{1,3}/;\n\ntype TinyColorToValue = {\n toHex: ColorValue;\n toHexString: ColorValue;\n toHsv: ColorValue;\n toHsvString: ColorValue;\n toHsl: ColorValue;\n toHslString: ColorValue;\n toHex8: ColorValue;\n toHex8String: ColorValue;\n toPercentageRgb: ColorValue;\n toPercentageRgbString: ColorValue;\n toRgb: ColorValue;\n toRgbString: ColorValue;\n};\n\nconst getHexValue = (color: TinyColor, isString: boolean): ColorValue =>\n isString ? color.toHexString() : color.toHex();\n\nexport class ColorController {\n protected host: ReactiveElement;\n\n protected applyColorToState!: ({\n h,\n s,\n v,\n }: {\n h: number;\n s: number;\n v: number;\n }) => void;\n\n protected extractColorFromState!: (\n controller: ColorController\n ) => ColorValue;\n\n protected setColorProcess(\n currentColor: TinyColor,\n nextColor: ColorValue,\n format: string,\n isString: boolean\n ): void {\n if (this.maintains === 'hue') {\n this.setColorMaintainHue(currentColor, nextColor, format, isString);\n } else if (this.maintains === 'saturation') {\n this.setColorMaintainSaturation(\n currentColor,\n nextColor,\n format,\n isString\n );\n }\n }\n\n protected setColorMaintainHue(\n currentColor: TinyColor,\n nextColor: ColorValue,\n format: string,\n isString: boolean\n ): void {\n const { h, s, v } = this._color.toHsv();\n let originalHue: number | undefined = undefined;\n\n if (isString && format.startsWith('hs')) {\n const values = extractHueAndSaturationRegExp.exec(\n nextColor as string\n );\n\n if (values !== null) {\n const [, h] = values;\n originalHue = Number(h);\n }\n } else if (!isString && format.startsWith('hs')) {\n const colorInput = currentColor.originalInput;\n const colorValues = Object.values(colorInput);\n originalHue = colorValues[0];\n }\n\n this.hue = originalHue || h;\n this.applyColorToState({ h, s, v });\n }\n\n protected setColorMaintainSaturation(\n currentColor: TinyColor,\n nextColor: ColorValue,\n format: string,\n isString: boolean\n ): void {\n if (isString && format.startsWith('hs')) {\n const values = extractHueAndSaturationRegExp.exec(\n nextColor as string\n );\n\n if (values !== null) {\n const [, h, s] = values;\n this.hue = Number(h);\n this.saturation = Number(s);\n }\n } else if (!isString && format.startsWith('hs')) {\n const colorInput = currentColor.originalInput;\n const colorValues = Object.values(colorInput);\n this.hue = colorValues[0];\n this.saturation = colorValues[1];\n } else {\n const { h } = currentColor.toHsv();\n this.hue = h;\n }\n this.applyColorToState(currentColor.toHsv());\n }\n\n protected maintains: 'hue' | 'saturation' = 'hue';\n private saturation!: number;\n\n constructor(\n host: ReactiveElement,\n {\n applyColorToState,\n extractColorFromState,\n maintains,\n }: {\n applyColorToState({\n h,\n s,\n v,\n }: {\n h: number;\n s: number;\n v: number;\n }): void;\n extractColorFromState(controller: ColorController): ColorValue;\n maintains?: 'hue' | 'saturation';\n }\n ) {\n this.host = host;\n this.applyColorToState = applyColorToState;\n this.extractColorFromState = extractColorFromState;\n this.maintains = maintains || this.maintains;\n }\n\n public applyColorFromState(): void {\n this._color = new TinyColor(this.extractColorFromState(this));\n }\n\n public get hue(): number {\n return this._hue;\n }\n\n public set hue(value: number) {\n const hue = Math.min(360, Math.max(0, value));\n if (hue === this.hue) {\n return;\n }\n const oldValue = this.hue;\n const { s, v } = this._color.toHsv();\n this._color = new TinyColor({ h: hue, s, v });\n this._hue = hue;\n this.host.requestUpdate('hue', oldValue);\n }\n\n private _hue = 0;\n\n protected getColorProcesses: Record<\n string,\n (color: TinyColor, isString: boolean) => ColorValue\n > = {\n rgb: (color, isString) =>\n isString ? color.toRgbString() : color.toRgb(),\n prgb: (color, isString) =>\n isString ? color.toPercentageRgbString() : color.toPercentageRgb(),\n hex8: (color, isString) =>\n isString ? color.toHex8String() : color.toHex8(),\n name: (color) => color.toName() || color.toRgbString(),\n hsl: (color, isString) => {\n if (this.maintains === 'hue') {\n if (isString) {\n const hslString = color.toHslString();\n return hslString.replace(replaceHueRegExp, `$1${this.hue}`);\n } else {\n const { s, l, a } = color.toHsl();\n return { h: this.hue, s, l, a };\n }\n } else {\n if (isString) {\n const hslString = color.toHslString();\n return hslString.replace(\n replaceHueAndSaturationRegExp,\n `$1${this.hue}$2${this.saturation}`\n );\n } else {\n const { s, l, a } = color.toHsl();\n return { h: this.hue, s, l, a };\n }\n }\n },\n hsv: (color, isString) => {\n if (this.maintains === 'hue') {\n if (isString) {\n const hsvString = color.toHsvString();\n return hsvString.replace(replaceHueRegExp, `$1${this.hue}`);\n } else {\n const { s, v, a } = color.toHsv();\n return { h: this.hue, s, v, a };\n }\n } else {\n if (isString) {\n const hsvString = color.toHsvString();\n return hsvString.replace(\n replaceHueAndSaturationRegExp,\n `$1${this.hue}$2${this.saturation}`\n );\n } else {\n const { s, v, a } = color.toHsv();\n return { h: this.hue, s, v, a };\n }\n }\n },\n hex: getHexValue,\n hex3: getHexValue,\n hex4: getHexValue,\n hex6: getHexValue,\n };\n\n /* c8 ignore next 3 */\n public get value(): ColorValue {\n return this.color;\n }\n\n public get color(): ColorValue {\n return this.getColorProcesses[this._format.format || 'hex'](\n this._color,\n this._format.isString\n );\n }\n\n public set color(color: ColorValue) {\n /* c8 ignore next 3 */\n if (color === this.color) {\n return;\n }\n const oldValue = this._color;\n this._color = new TinyColor(color);\n const format = this._color.format;\n let isString = typeof color === 'string' || color instanceof String;\n\n if (format.startsWith('hex')) {\n isString = (color as string).startsWith('#');\n }\n\n this._format = {\n format,\n isString,\n };\n\n this.setColorProcess(this._color, color, format, isString);\n this.host.requestUpdate('color', oldValue);\n }\n\n private _color = new TinyColor({ h: 0, s: 1, v: 1 });\n\n public getColor(format: string): ColorValue {\n const formatOptions: Record<string, keyof TinyColorToValue> = {\n hsl: 'toHsl',\n };\n return this._color[formatOptions[format]]();\n }\n\n public setColor(color: TinyColor): void {\n this._color = color;\n const isString =\n typeof this._color.originalInput === 'string' ||\n this._color.originalInput instanceof String;\n this.setColorProcess(this._color, color, this._color.format, isString);\n }\n\n public getHslString(): string {\n return this._color.toHslString();\n }\n\n private _previousColor = new TinyColor({ h: 0, s: 1, v: 1 });\n\n public savePreviousColor(): void {\n this._previousColor = this._color.clone();\n }\n\n public restorePreviousColor(): void {\n this.setColor(this._previousColor);\n }\n\n private _format: { format: string; isString: boolean } = {\n format: '',\n isString: false,\n };\n}\n"],
|
|
5
|
+
"mappings": "aAaA,OAA0C,aAAAA,MAAiB,kBAcpD,aAAM,8BACT,gDACS,8BACT,gDACS,iBAAmB,wBAiBhC,MAAMC,EAAc,CAACC,EAAkBC,IACnCA,EAAWD,EAAM,YAAY,EAAIA,EAAM,MAAM,EAE1C,aAAM,eAAgB,CA8FzB,YACIE,EACA,CACI,kBAAAC,EACA,sBAAAC,EACA,UAAAC,CACJ,EAaF,CAtBF,KAAU,UAAkC,MAiD5C,KAAQ,KAAO,EAEf,KAAU,kBAGN,CACA,IAAK,CAACL,EAAOC,IACTA,EAAWD,EAAM,YAAY,EAAIA,EAAM,MAAM,EACjD,KAAM,CAACA,EAAOC,IACVA,EAAWD,EAAM,sBAAsB,EAAIA,EAAM,gBAAgB,EACrE,KAAM,CAACA,EAAOC,IACVA,EAAWD,EAAM,aAAa,EAAIA,EAAM,OAAO,EACnD,KAAOA,GAAUA,EAAM,OAAO,GAAKA,EAAM,YAAY,EACrD,IAAK,CAACA,EAAOC,IAAa,CACtB,GAAI,KAAK,YAAc,MAAO,CAC1B,GAAIA,EAEA,OADkBD,EAAM,YAAY,EACnB,QAAQ,iBAAkB,KAAK,KAAK,KA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AAc,QAASmB,CAAQ,CAC7C,CAIO,SAASR,EAA4B,CACxC,MAAMS,EAAwD,CAC1D,IAAK,OACT,EACA,OAAO,KAAK,OAAOA,EAAcT,IAAS,CAC9C,CAEO,SAASZ,EAAwB,CACpC,KAAK,OAASA,EACd,MAAMC,EACF,OAAO,KAAK,OAAO,eAAkB,UACrC,KAAK,OAAO,yBAAyB,OACzC,KAAK,gBAAgB,KAAK,OAAQD,EAAO,KAAK,OAAO,OAAQC,CAAQ,CACzE,CAEO,cAAuB,CAC1B,OAAO,KAAK,OAAO,YAAY,CACnC,CAIO,mBAA0B,CAC7B,KAAK,eAAiB,KAAK,OAAO,MAAM,CAC5C,CAEO,sBAA6B,CAChC,KAAK,SAAS,KAAK,cAAc,CACrC,CAMJ",
|
|
6
6
|
"names": ["TinyColor", "getHexValue", "color", "isString", "host", "applyColorToState", "extractColorFromState", "maintains", "s", "l", "a", "v", "currentColor", "nextColor", "format", "h", "originalHue", "values", "colorInput", "colorValues", "value", "hue", "oldValue", "formatOptions"]
|
|
7
7
|
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { ReactiveController, ReactiveElement } from 'lit';
|
|
2
|
+
export declare const elementResolverUpdatedSymbol: unique symbol;
|
|
3
|
+
export declare class ElementResolutionController implements ReactiveController {
|
|
4
|
+
get element(): HTMLElement | null;
|
|
5
|
+
set element(element: HTMLElement | null);
|
|
6
|
+
private _element;
|
|
7
|
+
private host;
|
|
8
|
+
private observer;
|
|
9
|
+
get selector(): string;
|
|
10
|
+
set selector(selector: string);
|
|
11
|
+
private _selector;
|
|
12
|
+
constructor(host: ReactiveElement, { selector }?: {
|
|
13
|
+
selector: string;
|
|
14
|
+
});
|
|
15
|
+
protected mutationCallback: MutationCallback;
|
|
16
|
+
hostConnected(): void;
|
|
17
|
+
hostDisconnected(): void;
|
|
18
|
+
private resolveElement;
|
|
19
|
+
private releaseElement;
|
|
20
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
export const elementResolverUpdatedSymbol = Symbol("element resolver updated");
|
|
3
|
+
export class ElementResolutionController {
|
|
4
|
+
constructor(host, { selector } = { selector: "" }) {
|
|
5
|
+
this._element = null;
|
|
6
|
+
this._selector = "";
|
|
7
|
+
this.mutationCallback = (mutationList) => {
|
|
8
|
+
let needsResolution = false;
|
|
9
|
+
mutationList.forEach((mutation) => {
|
|
10
|
+
if (needsResolution)
|
|
11
|
+
return;
|
|
12
|
+
if (mutation.type === "childList") {
|
|
13
|
+
const currentElementRemoved = this.element && [...mutation.removedNodes].includes(this.element);
|
|
14
|
+
const matchingElementAdded = !!this.selector && [...mutation.addedNodes].some(
|
|
15
|
+
(el) => {
|
|
16
|
+
var _a;
|
|
17
|
+
return (_a = el == null ? void 0 : el.matches) == null ? void 0 : _a.call(el, this.selector);
|
|
18
|
+
}
|
|
19
|
+
);
|
|
20
|
+
needsResolution = needsResolution || currentElementRemoved || matchingElementAdded;
|
|
21
|
+
}
|
|
22
|
+
if (mutation.type === "attributes") {
|
|
23
|
+
const attributeChangedOnCurrentElement = mutation.target === this.element;
|
|
24
|
+
const attributeChangedOnMatchingElement = !!this.selector && mutation.target.matches(this.selector);
|
|
25
|
+
needsResolution = needsResolution || attributeChangedOnCurrentElement || attributeChangedOnMatchingElement;
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
if (needsResolution) {
|
|
29
|
+
this.resolveElement();
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
this.host = host;
|
|
33
|
+
this.selector = selector;
|
|
34
|
+
this.observer = new MutationObserver(this.mutationCallback);
|
|
35
|
+
this.host.addController(this);
|
|
36
|
+
}
|
|
37
|
+
get element() {
|
|
38
|
+
return this._element;
|
|
39
|
+
}
|
|
40
|
+
set element(element) {
|
|
41
|
+
if (element === this.element)
|
|
42
|
+
return;
|
|
43
|
+
const previous = this.element;
|
|
44
|
+
this._element = element;
|
|
45
|
+
this.host.requestUpdate(elementResolverUpdatedSymbol, previous);
|
|
46
|
+
}
|
|
47
|
+
get selector() {
|
|
48
|
+
return this._selector;
|
|
49
|
+
}
|
|
50
|
+
set selector(selector) {
|
|
51
|
+
if (selector === this.selector)
|
|
52
|
+
return;
|
|
53
|
+
this.releaseElement();
|
|
54
|
+
this._selector = selector;
|
|
55
|
+
this.resolveElement();
|
|
56
|
+
}
|
|
57
|
+
hostConnected() {
|
|
58
|
+
this.resolveElement();
|
|
59
|
+
this.observer.observe(this.host.getRootNode(), {
|
|
60
|
+
subtree: true,
|
|
61
|
+
childList: true,
|
|
62
|
+
attributes: true
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
hostDisconnected() {
|
|
66
|
+
this.releaseElement();
|
|
67
|
+
this.observer.disconnect();
|
|
68
|
+
}
|
|
69
|
+
resolveElement() {
|
|
70
|
+
if (!this.selector) {
|
|
71
|
+
this.releaseElement();
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
const parent = this.host.getRootNode();
|
|
75
|
+
this.element = parent.querySelector(this.selector);
|
|
76
|
+
}
|
|
77
|
+
releaseElement() {
|
|
78
|
+
this.element = null;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
//# sourceMappingURL=ElementResolution.dev.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["ElementResolution.ts"],
|
|
4
|
+
"sourcesContent": ["/*\nCopyright 2022 Adobe. All rights reserved.\nThis file is licensed to you under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License. You may obtain a copy\nof the License at http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under\nthe License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\nOF ANY KIND, either express or implied. See the License for the specific language\ngoverning permissions and limitations under the License.\n*/\n\nimport type { ReactiveController, ReactiveElement } from 'lit';\nexport const elementResolverUpdatedSymbol = Symbol('element resolver updated');\n\nexport class ElementResolutionController implements ReactiveController {\n get element(): HTMLElement | null {\n return this._element;\n }\n\n set element(element: HTMLElement | null) {\n if (element === this.element) return;\n const previous = this.element;\n this._element = element;\n // requestUpdate leveraging the exported Symbol() so that the\n // changes can be easily tracked in the host element.\n this.host.requestUpdate(elementResolverUpdatedSymbol, previous);\n }\n\n private _element: HTMLElement | null = null;\n\n private host!: ReactiveElement;\n\n private observer!: MutationObserver;\n\n get selector(): string {\n return this._selector;\n }\n\n set selector(selector: string) {\n if (selector === this.selector) return;\n this.releaseElement();\n this._selector = selector;\n this.resolveElement();\n }\n\n private _selector = '';\n\n constructor(\n host: ReactiveElement,\n { selector }: { selector: string } = { selector: '' }\n ) {\n this.host = host;\n this.selector = selector;\n this.observer = new MutationObserver(this.mutationCallback);\n // Add the controller after the MutationObserver has been created in preparation\n // for the `hostConnected`/`hostDisconnected` callbacks to be run.\n this.host.addController(this);\n }\n\n protected mutationCallback: MutationCallback = (mutationList) => {\n let needsResolution = false;\n mutationList.forEach((mutation) => {\n if (needsResolution) return;\n if (mutation.type === 'childList') {\n const currentElementRemoved =\n this.element &&\n [...mutation.removedNodes].includes(this.element);\n const matchingElementAdded =\n !!this.selector &&\n ([...mutation.addedNodes] as HTMLElement[]).some((el) =>\n el?.matches?.(this.selector)\n );\n needsResolution =\n needsResolution ||\n currentElementRemoved ||\n matchingElementAdded;\n }\n if (mutation.type === 'attributes') {\n const attributeChangedOnCurrentElement =\n mutation.target === this.element;\n const attributeChangedOnMatchingElement =\n !!this.selector &&\n (mutation.target as HTMLElement).matches(this.selector);\n needsResolution =\n needsResolution ||\n attributeChangedOnCurrentElement ||\n attributeChangedOnMatchingElement;\n }\n });\n if (needsResolution) {\n this.resolveElement();\n }\n };\n\n public hostConnected(): void {\n this.resolveElement();\n this.observer.observe(this.host.getRootNode(), {\n subtree: true,\n childList: true,\n attributes: true,\n });\n }\n\n public hostDisconnected(): void {\n this.releaseElement();\n this.observer.disconnect();\n }\n\n private resolveElement(): void {\n if (!this.selector) {\n this.releaseElement();\n return;\n }\n\n const parent = this.host.getRootNode() as HTMLElement;\n this.element = parent.querySelector(this.selector) as HTMLElement;\n }\n\n private releaseElement(): void {\n this.element = null;\n }\n}\n"],
|
|
5
|
+
"mappings": ";AAaO,aAAM,+BAA+B,OAAO,0BAA0B;AAEtE,aAAM,4BAA0D;AAAA,EAiCnE,YACI,MACA,EAAE,SAAS,IAA0B,EAAE,UAAU,GAAG,GACtD;AAtBF,SAAQ,WAA+B;AAiBvC,SAAQ,YAAY;AAcpB,SAAU,mBAAqC,CAAC,iBAAiB;AAC7D,UAAI,kBAAkB;AACtB,mBAAa,QAAQ,CAAC,aAAa;AAC/B,YAAI;AAAiB;AACrB,YAAI,SAAS,SAAS,aAAa;AAC/B,gBAAM,wBACF,KAAK,WACL,CAAC,GAAG,SAAS,YAAY,EAAE,SAAS,KAAK,OAAO;AACpD,gBAAM,uBACF,CAAC,CAAC,KAAK,YACN,CAAC,GAAG,SAAS,UAAU,EAAoB;AAAA,YAAK,CAAC,OAAI;AAtE1E;AAuEwB,oDAAI,YAAJ,4BAAc,KAAK;AAAA;AAAA,UACvB;AACJ,4BACI,mBACA,yBACA;AAAA,QACR;AACA,YAAI,SAAS,SAAS,cAAc;AAChC,gBAAM,mCACF,SAAS,WAAW,KAAK;AAC7B,gBAAM,oCACF,CAAC,CAAC,KAAK,YACN,SAAS,OAAuB,QAAQ,KAAK,QAAQ;AAC1D,4BACI,mBACA,oCACA;AAAA,QACR;AAAA,MACJ,CAAC;AACD,UAAI,iBAAiB;AACjB,aAAK,eAAe;AAAA,MACxB;AAAA,IACJ;AAzCI,SAAK,OAAO;AACZ,SAAK,WAAW;AAChB,SAAK,WAAW,IAAI,iBAAiB,KAAK,gBAAgB;AAG1D,SAAK,KAAK,cAAc,IAAI;AAAA,EAChC;AAAA,EA1CA,IAAI,UAA8B;AAC9B,WAAO,KAAK;AAAA,EAChB;AAAA,EAEA,IAAI,QAAQ,SAA6B;AACrC,QAAI,YAAY,KAAK;AAAS;AAC9B,UAAM,WAAW,KAAK;AACtB,SAAK,WAAW;AAGhB,SAAK,KAAK,cAAc,8BAA8B,QAAQ;AAAA,EAClE;AAAA,EAQA,IAAI,WAAmB;AACnB,WAAO,KAAK;AAAA,EAChB;AAAA,EAEA,IAAI,SAAS,UAAkB;AAC3B,QAAI,aAAa,KAAK;AAAU;AAChC,SAAK,eAAe;AACpB,SAAK,YAAY;AACjB,SAAK,eAAe;AAAA,EACxB;AAAA,EAmDO,gBAAsB;AACzB,SAAK,eAAe;AACpB,SAAK,SAAS,QAAQ,KAAK,KAAK,YAAY,GAAG;AAAA,MAC3C,SAAS;AAAA,MACT,WAAW;AAAA,MACX,YAAY;AAAA,IAChB,CAAC;AAAA,EACL;AAAA,EAEO,mBAAyB;AAC5B,SAAK,eAAe;AACpB,SAAK,SAAS,WAAW;AAAA,EAC7B;AAAA,EAEQ,iBAAuB;AAC3B,QAAI,CAAC,KAAK,UAAU;AAChB,WAAK,eAAe;AACpB;AAAA,IACJ;AAEA,UAAM,SAAS,KAAK,KAAK,YAAY;AACrC,SAAK,UAAU,OAAO,cAAc,KAAK,QAAQ;AAAA,EACrD;AAAA,EAEQ,iBAAuB;AAC3B,SAAK,UAAU;AAAA,EACnB;AACJ;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";export const elementResolverUpdatedSymbol=Symbol("element resolver updated");export class ElementResolutionController{constructor(e,{selector:t}={selector:""}){this._element=null;this._selector="";this.mutationCallback=e=>{let t=!1;e.forEach(s=>{if(!t){if(s.type==="childList"){const l=this.element&&[...s.removedNodes].includes(this.element),n=!!this.selector&&[...s.addedNodes].some(r=>{var o;return(o=r==null?void 0:r.matches)==null?void 0:o.call(r,this.selector)});t=t||l||n}if(s.type==="attributes"){const l=s.target===this.element,n=!!this.selector&&s.target.matches(this.selector);t=t||l||n}}}),t&&this.resolveElement()};this.host=e,this.selector=t,this.observer=new MutationObserver(this.mutationCallback),this.host.addController(this)}get element(){return this._element}set element(e){if(e===this.element)return;const t=this.element;this._element=e,this.host.requestUpdate(elementResolverUpdatedSymbol,t)}get selector(){return this._selector}set selector(e){e!==this.selector&&(this.releaseElement(),this._selector=e,this.resolveElement())}hostConnected(){this.resolveElement(),this.observer.observe(this.host.getRootNode(),{subtree:!0,childList:!0,attributes:!0})}hostDisconnected(){this.releaseElement(),this.observer.disconnect()}resolveElement(){if(!this.selector){this.releaseElement();return}const e=this.host.getRootNode();this.element=e.querySelector(this.selector)}releaseElement(){this.element=null}}
|
|
2
|
+
//# sourceMappingURL=ElementResolution.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["ElementResolution.ts"],
|
|
4
|
+
"sourcesContent": ["/*\nCopyright 2022 Adobe. All rights reserved.\nThis file is licensed to you under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License. You may obtain a copy\nof the License at http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under\nthe License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\nOF ANY KIND, either express or implied. See the License for the specific language\ngoverning permissions and limitations under the License.\n*/\n\nimport type { ReactiveController, ReactiveElement } from 'lit';\nexport const elementResolverUpdatedSymbol = Symbol('element resolver updated');\n\nexport class ElementResolutionController implements ReactiveController {\n get element(): HTMLElement | null {\n return this._element;\n }\n\n set element(element: HTMLElement | null) {\n if (element === this.element) return;\n const previous = this.element;\n this._element = element;\n // requestUpdate leveraging the exported Symbol() so that the\n // changes can be easily tracked in the host element.\n this.host.requestUpdate(elementResolverUpdatedSymbol, previous);\n }\n\n private _element: HTMLElement | null = null;\n\n private host!: ReactiveElement;\n\n private observer!: MutationObserver;\n\n get selector(): string {\n return this._selector;\n }\n\n set selector(selector: string) {\n if (selector === this.selector) return;\n this.releaseElement();\n this._selector = selector;\n this.resolveElement();\n }\n\n private _selector = '';\n\n constructor(\n host: ReactiveElement,\n { selector }: { selector: string } = { selector: '' }\n ) {\n this.host = host;\n this.selector = selector;\n this.observer = new MutationObserver(this.mutationCallback);\n // Add the controller after the MutationObserver has been created in preparation\n // for the `hostConnected`/`hostDisconnected` callbacks to be run.\n this.host.addController(this);\n }\n\n protected mutationCallback: MutationCallback = (mutationList) => {\n let needsResolution = false;\n mutationList.forEach((mutation) => {\n if (needsResolution) return;\n if (mutation.type === 'childList') {\n const currentElementRemoved =\n this.element &&\n [...mutation.removedNodes].includes(this.element);\n const matchingElementAdded =\n !!this.selector &&\n ([...mutation.addedNodes] as HTMLElement[]).some((el) =>\n el?.matches?.(this.selector)\n );\n needsResolution =\n needsResolution ||\n currentElementRemoved ||\n matchingElementAdded;\n }\n if (mutation.type === 'attributes') {\n const attributeChangedOnCurrentElement =\n mutation.target === this.element;\n const attributeChangedOnMatchingElement =\n !!this.selector &&\n (mutation.target as HTMLElement).matches(this.selector);\n needsResolution =\n needsResolution ||\n attributeChangedOnCurrentElement ||\n attributeChangedOnMatchingElement;\n }\n });\n if (needsResolution) {\n this.resolveElement();\n }\n };\n\n public hostConnected(): void {\n this.resolveElement();\n this.observer.observe(this.host.getRootNode(), {\n subtree: true,\n childList: true,\n attributes: true,\n });\n }\n\n public hostDisconnected(): void {\n this.releaseElement();\n this.observer.disconnect();\n }\n\n private resolveElement(): void {\n if (!this.selector) {\n this.releaseElement();\n return;\n }\n\n const parent = this.host.getRootNode() as HTMLElement;\n this.element = parent.querySelector(this.selector) as HTMLElement;\n }\n\n private releaseElement(): void {\n this.element = null;\n }\n}\n"],
|
|
5
|
+
"mappings": "aAaO,aAAM,6BAA+B,OAAO,0BAA0B,EAEtE,aAAM,2BAA0D,CAiCnE,YACIA,EACA,CAAE,SAAAC,CAAS,EAA0B,CAAE,SAAU,EAAG,EACtD,CAtBF,KAAQ,SAA+B,KAiBvC,KAAQ,UAAY,GAcpB,KAAU,iBAAsCC,GAAi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KAAK,eAAe,EACpB,KAAK,SAAS,WAAW,CAC7B,CAEQ,gBAAuB,CAC3B,GAAI,CAAC,KAAK,SAAU,CAChB,KAAK,eAAe,EACpB,MACJ,CAEA,MAAMY,EAAS,KAAK,KAAK,YAAY,EACrC,KAAK,QAAUA,EAAO,cAAc,KAAK,QAAQ,CACrD,CAEQ,gBAAuB,CAC3B,KAAK,QAAU,IACnB,CACJ",
|
|
6
|
+
"names": ["host", "selector", "mutationList", "needsResolution", "mutation", "currentElementRemoved", "matchingElementAdded", "el", "_a", "attributeChangedOnCurrentElement", "attributeChangedOnMatchingElement", "element", "previous", "parent"]
|
|
7
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["FocusGroup.ts"],
|
|
4
|
-
"sourcesContent": ["/*\nCopyright 2020 Adobe. All rights reserved.\nThis file is licensed to you under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License. You may obtain a copy\nof the License at http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under\nthe License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\nOF ANY KIND, either express or implied. See the License for the specific language\ngoverning permissions and limitations under the License.\n*/\nimport type { ReactiveController, ReactiveElement } from 'lit';\n\ntype DirectionTypes = 'horizontal' | 'vertical' | 'both' | 'grid';\nexport type FocusGroupConfig<T> = {\n focusInIndex?: (_elements: T[]) => number;\n direction?: DirectionTypes | (() => DirectionTypes);\n elementEnterAction?: (el: T) => void;\n elements: () => T[];\n isFocusableElement?: (el: T) => boolean;\n listenerScope?: HTMLElement | (() => HTMLElement);\n};\n\nfunction ensureMethod<T, RT>(\n value: T | RT | undefined,\n type: string,\n fallback: T\n): T {\n if (typeof value === type) {\n return (() => value) as T;\n } else if (typeof value === 'function') {\n return value as T;\n }\n return fallback;\n}\n\nexport class FocusGroupController<T extends HTMLElement>\n implements ReactiveController\n{\n protected cachedElements?: T[];\n\n get currentIndex(): number {\n if (this._currentIndex === -1) {\n this._currentIndex = this.focusInIndex;\n }\n return this._currentIndex - this.offset;\n }\n\n set currentIndex(currentIndex) {\n this._currentIndex = currentIndex + this.offset;\n }\n\n private _currentIndex = -1;\n\n get direction(): DirectionTypes {\n return this._direction();\n }\n\n _direction = (): DirectionTypes => 'both';\n\n public directionLength = 5;\n\n elementEnterAction = (_el: T): void => {\n return;\n };\n\n get elements(): T[] {\n if (!this.cachedElements) {\n this.cachedElements = this._elements();\n }\n return this.cachedElements;\n }\n\n private _elements!: () => T[];\n\n protected set focused(focused: boolean) {\n if (focused === this.focused) return;\n this._focused = focused;\n }\n\n protected get focused(): boolean {\n return this._focused;\n }\n\n private _focused = false;\n\n get focusInElement(): T {\n return this.elements[this.focusInIndex];\n }\n\n get focusInIndex(): number {\n return this._focusInIndex(this.elements);\n }\n\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n _focusInIndex = (_elements: T[]): number => 0;\n\n host: ReactiveElement;\n\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n isFocusableElement = (_el: T): boolean => true;\n\n isEventWithinListenerScope(event: Event): boolean {\n if (this._listenerScope() === this.host) return true;\n return event.composedPath().includes(this._listenerScope());\n }\n\n _listenerScope = (): HTMLElement => this.host;\n\n // When elements are virtualized, the delta between the first element\n // and the first rendered element.\n offset = 0;\n\n constructor(\n host: ReactiveElement,\n {\n direction,\n elementEnterAction,\n elements,\n focusInIndex,\n isFocusableElement,\n listenerScope,\n }: FocusGroupConfig<T> = { elements: () => [] }\n ) {\n this.host = host;\n this.host.addController(this);\n this._elements = elements;\n this.isFocusableElement = isFocusableElement || this.isFocusableElement;\n this._direction = ensureMethod<() => DirectionTypes, DirectionTypes>(\n direction,\n 'string',\n this._direction\n );\n this.elementEnterAction = elementEnterAction || this.elementEnterAction;\n this._focusInIndex = ensureMethod<(_elements: T[]) => number, number>(\n focusInIndex,\n 'number',\n this._focusInIndex\n );\n this._listenerScope = ensureMethod<() => HTMLElement, HTMLElement>(\n listenerScope,\n 'object',\n this._listenerScope\n );\n }\n\n update({ elements }: FocusGroupConfig<T> = { elements: () => [] }): void {\n this.unmanage();\n this._elements = elements;\n this.clearElementCache();\n this.manage();\n }\n\n focus(options?: FocusOptions): void {\n let focusElement = this.elements[this.currentIndex];\n if (!focusElement || !this.isFocusableElement(focusElement)) {\n this.setCurrentIndexCircularly(1);\n focusElement = this.elements[this.currentIndex];\n }\n if (focusElement && this.isFocusableElement(focusElement)) {\n focusElement.focus(options);\n }\n }\n\n clearElementCache(offset = 0): void {\n delete this.cachedElements;\n this.offset = offset;\n }\n\n setCurrentIndexCircularly(diff: number): void {\n const { length } = this.elements;\n let steps = length;\n // start at a possibly not 0 index\n let nextIndex = (length + this.currentIndex + diff) % length;\n while (\n // don't cycle the elements more than once\n steps &&\n this.elements[nextIndex] &&\n !this.isFocusableElement(this.elements[nextIndex])\n ) {\n nextIndex = (length + nextIndex + diff) % length;\n steps -= 1;\n }\n this.currentIndex = nextIndex;\n }\n\n hostContainsFocus(): void {\n this.host.addEventListener('focusout', this.handleFocusout);\n this.host.addEventListener('keydown', this.handleKeydown);\n this.focused = true;\n }\n\n hostNoLongerContainsFocus(): void {\n this.host.addEventListener('focusin', this.handleFocusin);\n this.host.removeEventListener('focusout', this.handleFocusout);\n this.host.removeEventListener('keydown', this.handleKeydown);\n this.currentIndex = this.focusInIndex;\n this.focused = false;\n }\n\n isRelatedTargetAnElement(event: FocusEvent): boolean {\n const relatedTarget = event.relatedTarget as null | Element;\n return !this.elements.includes(relatedTarget as T);\n }\n\n handleFocusin = (event: FocusEvent): void => {\n if (!this.isEventWithinListenerScope(event)) return;\n if (this.isRelatedTargetAnElement(event)) {\n this.hostContainsFocus();\n }\n const path = event.composedPath() as T[];\n let targetIndex = -1;\n path.find((el) => {\n targetIndex = this.elements.indexOf(el);\n return targetIndex !== -1;\n });\n this.currentIndex = targetIndex > -1 ? targetIndex : this.currentIndex;\n };\n\n handleFocusout = (event: FocusEvent): void => {\n if (this.isRelatedTargetAnElement(event)) {\n this.hostNoLongerContainsFocus();\n }\n };\n\n acceptsEventCode(code: string): boolean {\n if (code === 'End' || code === 'Home') {\n return true;\n }\n switch (this.direction) {\n case 'horizontal':\n return code === 'ArrowLeft' || code === 'ArrowRight';\n case 'vertical':\n return code === 'ArrowUp' || code === 'ArrowDown';\n case 'both':\n case 'grid':\n return code.startsWith('Arrow');\n }\n }\n\n handleKeydown = (event: KeyboardEvent): void => {\n if (!this.acceptsEventCode(event.code) || event.defaultPrevented) {\n return;\n }\n let diff = 0;\n switch (event.code) {\n case 'ArrowRight':\n diff += 1;\n break;\n case 'ArrowDown':\n diff += this.direction === 'grid' ? this.directionLength : 1;\n break;\n case 'ArrowLeft':\n diff -= 1;\n break;\n case 'ArrowUp':\n diff -= this.direction === 'grid' ? this.directionLength : 1;\n break;\n case 'End':\n this.currentIndex = 0;\n diff -= 1;\n break;\n case 'Home':\n this.currentIndex = this.elements.length - 1;\n diff += 1;\n break;\n }\n event.preventDefault();\n if (this.direction === 'grid' && this.currentIndex + diff < 0) {\n this.currentIndex = 0;\n } else if (\n this.direction === 'grid' &&\n this.currentIndex + diff > this.elements.length - 1\n ) {\n this.currentIndex = this.elements.length - 1;\n } else {\n this.setCurrentIndexCircularly(diff);\n }\n // To allow the `focusInIndex` to be calculated with the \"after\" state of the keyboard interaction\n // do `elementEnterAction` _before_ focusing the next element.\n this.elementEnterAction(this.elements[this.currentIndex]);\n this.focus();\n };\n\n manage(): void {\n this.addEventListeners();\n }\n\n unmanage(): void {\n this.removeEventListeners();\n }\n\n addEventListeners(): void {\n this.host.addEventListener('focusin', this.handleFocusin);\n }\n\n removeEventListeners(): void {\n this.host.removeEventListener('focusin', this.handleFocusin);\n this.host.removeEventListener('focusout', this.handleFocusout);\n this.host.removeEventListener('keydown', this.handleKeydown);\n }\n\n hostConnected(): void {\n this.addEventListeners();\n }\n\n hostDisconnected(): void {\n this.removeEventListeners();\n }\n}\n"],
|
|
5
|
-
"mappings": ";AAuBA,SAAS,aACL,OACA,MACA,UACC;AACD,MAAI,OAAO,UAAU,MAAM;AACvB,WAAQ,MAAM;AAAA,EAClB,WAAW,OAAO,UAAU,YAAY;AACpC,WAAO;AAAA,EACX;AACA,SAAO;AACX;AAEO,aAAM,qBAEb;AAAA,
|
|
4
|
+
"sourcesContent": ["/*\nCopyright 2020 Adobe. All rights reserved.\nThis file is licensed to you under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License. You may obtain a copy\nof the License at http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under\nthe License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\nOF ANY KIND, either express or implied. See the License for the specific language\ngoverning permissions and limitations under the License.\n*/\nimport type { ReactiveController, ReactiveElement } from 'lit';\n\ntype DirectionTypes = 'horizontal' | 'vertical' | 'both' | 'grid';\nexport type FocusGroupConfig<T> = {\n focusInIndex?: (_elements: T[]) => number;\n direction?: DirectionTypes | (() => DirectionTypes);\n elementEnterAction?: (el: T) => void;\n elements: () => T[];\n isFocusableElement?: (el: T) => boolean;\n listenerScope?: HTMLElement | (() => HTMLElement);\n};\n\nfunction ensureMethod<T, RT>(\n value: T | RT | undefined,\n type: string,\n fallback: T\n): T {\n if (typeof value === type) {\n return (() => value) as T;\n } else if (typeof value === 'function') {\n return value as T;\n }\n return fallback;\n}\n\nexport class FocusGroupController<T extends HTMLElement>\n implements ReactiveController\n{\n protected cachedElements?: T[];\n\n get currentIndex(): number {\n if (this._currentIndex === -1) {\n this._currentIndex = this.focusInIndex;\n }\n return this._currentIndex - this.offset;\n }\n\n set currentIndex(currentIndex) {\n this._currentIndex = currentIndex + this.offset;\n }\n\n private _currentIndex = -1;\n\n get direction(): DirectionTypes {\n return this._direction();\n }\n\n _direction = (): DirectionTypes => 'both';\n\n public directionLength = 5;\n\n elementEnterAction = (_el: T): void => {\n return;\n };\n\n get elements(): T[] {\n if (!this.cachedElements) {\n this.cachedElements = this._elements();\n }\n return this.cachedElements;\n }\n\n private _elements!: () => T[];\n\n protected set focused(focused: boolean) {\n /* c8 ignore next 1 */\n if (focused === this.focused) return;\n this._focused = focused;\n }\n\n protected get focused(): boolean {\n return this._focused;\n }\n\n private _focused = false;\n\n get focusInElement(): T {\n return this.elements[this.focusInIndex];\n }\n\n get focusInIndex(): number {\n return this._focusInIndex(this.elements);\n }\n\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n _focusInIndex = (_elements: T[]): number => 0;\n\n host: ReactiveElement;\n\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n isFocusableElement = (_el: T): boolean => true;\n\n isEventWithinListenerScope(event: Event): boolean {\n if (this._listenerScope() === this.host) return true;\n return event.composedPath().includes(this._listenerScope());\n }\n\n _listenerScope = (): HTMLElement => this.host;\n\n // When elements are virtualized, the delta between the first element\n // and the first rendered element.\n offset = 0;\n\n constructor(\n host: ReactiveElement,\n {\n direction,\n elementEnterAction,\n elements,\n focusInIndex,\n isFocusableElement,\n listenerScope,\n }: FocusGroupConfig<T> = { elements: () => [] }\n ) {\n this.host = host;\n this.host.addController(this);\n this._elements = elements;\n this.isFocusableElement = isFocusableElement || this.isFocusableElement;\n this._direction = ensureMethod<() => DirectionTypes, DirectionTypes>(\n direction,\n 'string',\n this._direction\n );\n this.elementEnterAction = elementEnterAction || this.elementEnterAction;\n this._focusInIndex = ensureMethod<(_elements: T[]) => number, number>(\n focusInIndex,\n 'number',\n this._focusInIndex\n );\n this._listenerScope = ensureMethod<() => HTMLElement, HTMLElement>(\n listenerScope,\n 'object',\n this._listenerScope\n );\n }\n\n update({ elements }: FocusGroupConfig<T> = { elements: () => [] }): void {\n this.unmanage();\n this._elements = elements;\n this.clearElementCache();\n this.manage();\n }\n\n focus(options?: FocusOptions): void {\n let focusElement = this.elements[this.currentIndex];\n if (!focusElement || !this.isFocusableElement(focusElement)) {\n this.setCurrentIndexCircularly(1);\n focusElement = this.elements[this.currentIndex];\n }\n if (focusElement && this.isFocusableElement(focusElement)) {\n focusElement.focus(options);\n }\n }\n\n clearElementCache(offset = 0): void {\n delete this.cachedElements;\n this.offset = offset;\n }\n\n setCurrentIndexCircularly(diff: number): void {\n const { length } = this.elements;\n let steps = length;\n // start at a possibly not 0 index\n let nextIndex = (length + this.currentIndex + diff) % length;\n while (\n // don't cycle the elements more than once\n steps &&\n this.elements[nextIndex] &&\n !this.isFocusableElement(this.elements[nextIndex])\n ) {\n nextIndex = (length + nextIndex + diff) % length;\n steps -= 1;\n }\n this.currentIndex = nextIndex;\n }\n\n hostContainsFocus(): void {\n this.host.addEventListener('focusout', this.handleFocusout);\n this.host.addEventListener('keydown', this.handleKeydown);\n this.focused = true;\n }\n\n hostNoLongerContainsFocus(): void {\n this.host.addEventListener('focusin', this.handleFocusin);\n this.host.removeEventListener('focusout', this.handleFocusout);\n this.host.removeEventListener('keydown', this.handleKeydown);\n this.currentIndex = this.focusInIndex;\n this.focused = false;\n }\n\n isRelatedTargetAnElement(event: FocusEvent): boolean {\n const relatedTarget = event.relatedTarget as null | Element;\n return !this.elements.includes(relatedTarget as T);\n }\n\n handleFocusin = (event: FocusEvent): void => {\n if (!this.isEventWithinListenerScope(event)) return;\n if (this.isRelatedTargetAnElement(event)) {\n this.hostContainsFocus();\n }\n const path = event.composedPath() as T[];\n let targetIndex = -1;\n path.find((el) => {\n targetIndex = this.elements.indexOf(el);\n return targetIndex !== -1;\n });\n this.currentIndex = targetIndex > -1 ? targetIndex : this.currentIndex;\n };\n\n handleFocusout = (event: FocusEvent): void => {\n if (this.isRelatedTargetAnElement(event)) {\n this.hostNoLongerContainsFocus();\n }\n };\n\n acceptsEventCode(code: string): boolean {\n if (code === 'End' || code === 'Home') {\n return true;\n }\n switch (this.direction) {\n case 'horizontal':\n return code === 'ArrowLeft' || code === 'ArrowRight';\n case 'vertical':\n return code === 'ArrowUp' || code === 'ArrowDown';\n case 'both':\n case 'grid':\n return code.startsWith('Arrow');\n }\n }\n\n handleKeydown = (event: KeyboardEvent): void => {\n if (!this.acceptsEventCode(event.code) || event.defaultPrevented) {\n return;\n }\n let diff = 0;\n switch (event.code) {\n case 'ArrowRight':\n diff += 1;\n break;\n case 'ArrowDown':\n diff += this.direction === 'grid' ? this.directionLength : 1;\n break;\n case 'ArrowLeft':\n diff -= 1;\n break;\n case 'ArrowUp':\n diff -= this.direction === 'grid' ? this.directionLength : 1;\n break;\n case 'End':\n this.currentIndex = 0;\n diff -= 1;\n break;\n case 'Home':\n this.currentIndex = this.elements.length - 1;\n diff += 1;\n break;\n }\n event.preventDefault();\n if (this.direction === 'grid' && this.currentIndex + diff < 0) {\n this.currentIndex = 0;\n } else if (\n this.direction === 'grid' &&\n this.currentIndex + diff > this.elements.length - 1\n ) {\n this.currentIndex = this.elements.length - 1;\n } else {\n this.setCurrentIndexCircularly(diff);\n }\n // To allow the `focusInIndex` to be calculated with the \"after\" state of the keyboard interaction\n // do `elementEnterAction` _before_ focusing the next element.\n this.elementEnterAction(this.elements[this.currentIndex]);\n this.focus();\n };\n\n manage(): void {\n this.addEventListeners();\n }\n\n unmanage(): void {\n this.removeEventListeners();\n }\n\n addEventListeners(): void {\n this.host.addEventListener('focusin', this.handleFocusin);\n }\n\n removeEventListeners(): void {\n this.host.removeEventListener('focusin', this.handleFocusin);\n this.host.removeEventListener('focusout', this.handleFocusout);\n this.host.removeEventListener('keydown', this.handleKeydown);\n }\n\n hostConnected(): void {\n this.addEventListeners();\n }\n\n hostDisconnected(): void {\n this.removeEventListeners();\n }\n}\n"],
|
|
5
|
+
"mappings": ";AAuBA,SAAS,aACL,OACA,MACA,UACC;AACD,MAAI,OAAO,UAAU,MAAM;AACvB,WAAQ,MAAM;AAAA,EAClB,WAAW,OAAO,UAAU,YAAY;AACpC,WAAO;AAAA,EACX;AACA,SAAO;AACX;AAEO,aAAM,qBAEb;AAAA,EA4EI,YACI,MACA;AAAA,IACI;AAAA,IACA;AAAA,IACA;AAAA,IACA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ocusGroup.ts"],
|
|
4
|
-
"sourcesContent": ["/*\nCopyright 2020 Adobe. All rights reserved.\nThis file is licensed to you under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License. You may obtain a copy\nof the License at http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under\nthe License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\nOF ANY KIND, either express or implied. See the License for the specific language\ngoverning permissions and limitations under the License.\n*/\nimport type { ReactiveController, ReactiveElement } from 'lit';\n\ntype DirectionTypes = 'horizontal' | 'vertical' | 'both' | 'grid';\nexport type FocusGroupConfig<T> = {\n focusInIndex?: (_elements: T[]) => number;\n direction?: DirectionTypes | (() => DirectionTypes);\n elementEnterAction?: (el: T) => void;\n elements: () => T[];\n isFocusableElement?: (el: T) => boolean;\n listenerScope?: HTMLElement | (() => HTMLElement);\n};\n\nfunction ensureMethod<T, RT>(\n value: T | RT | undefined,\n type: string,\n fallback: T\n): T {\n if (typeof value === type) {\n return (() => value) as T;\n } else if (typeof value === 'function') {\n return value as T;\n }\n return fallback;\n}\n\nexport class FocusGroupController<T extends HTMLElement>\n implements ReactiveController\n{\n protected cachedElements?: T[];\n\n get currentIndex(): number {\n if (this._currentIndex === -1) {\n this._currentIndex = this.focusInIndex;\n }\n return this._currentIndex - this.offset;\n }\n\n set currentIndex(currentIndex) {\n this._currentIndex = currentIndex + this.offset;\n }\n\n private _currentIndex = -1;\n\n get direction(): DirectionTypes {\n return this._direction();\n }\n\n _direction = (): DirectionTypes => 'both';\n\n public directionLength = 5;\n\n elementEnterAction = (_el: T): void => {\n return;\n };\n\n get elements(): T[] {\n if (!this.cachedElements) {\n this.cachedElements = this._elements();\n }\n return this.cachedElements;\n }\n\n private _elements!: () => T[];\n\n protected set focused(focused: boolean) {\n if (focused === this.focused) return;\n this._focused = focused;\n }\n\n protected get focused(): boolean {\n return this._focused;\n }\n\n private _focused = false;\n\n get focusInElement(): T {\n return this.elements[this.focusInIndex];\n }\n\n get focusInIndex(): number {\n return this._focusInIndex(this.elements);\n }\n\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n _focusInIndex = (_elements: T[]): number => 0;\n\n host: ReactiveElement;\n\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n isFocusableElement = (_el: T): boolean => true;\n\n isEventWithinListenerScope(event: Event): boolean {\n if (this._listenerScope() === this.host) return true;\n return event.composedPath().includes(this._listenerScope());\n }\n\n _listenerScope = (): HTMLElement => this.host;\n\n // When elements are virtualized, the delta between the first element\n // and the first rendered element.\n offset = 0;\n\n constructor(\n host: ReactiveElement,\n {\n direction,\n elementEnterAction,\n elements,\n focusInIndex,\n isFocusableElement,\n listenerScope,\n }: FocusGroupConfig<T> = { elements: () => [] }\n ) {\n this.host = host;\n this.host.addController(this);\n this._elements = elements;\n this.isFocusableElement = isFocusableElement || this.isFocusableElement;\n this._direction = ensureMethod<() => DirectionTypes, DirectionTypes>(\n direction,\n 'string',\n this._direction\n );\n this.elementEnterAction = elementEnterAction || this.elementEnterAction;\n this._focusInIndex = ensureMethod<(_elements: T[]) => number, number>(\n focusInIndex,\n 'number',\n this._focusInIndex\n );\n this._listenerScope = ensureMethod<() => HTMLElement, HTMLElement>(\n listenerScope,\n 'object',\n this._listenerScope\n );\n }\n\n update({ elements }: FocusGroupConfig<T> = { elements: () => [] }): void {\n this.unmanage();\n this._elements = elements;\n this.clearElementCache();\n this.manage();\n }\n\n focus(options?: FocusOptions): void {\n let focusElement = this.elements[this.currentIndex];\n if (!focusElement || !this.isFocusableElement(focusElement)) {\n this.setCurrentIndexCircularly(1);\n focusElement = this.elements[this.currentIndex];\n }\n if (focusElement && this.isFocusableElement(focusElement)) {\n focusElement.focus(options);\n }\n }\n\n clearElementCache(offset = 0): void {\n delete this.cachedElements;\n this.offset = offset;\n }\n\n setCurrentIndexCircularly(diff: number): void {\n const { length } = this.elements;\n let steps = length;\n // start at a possibly not 0 index\n let nextIndex = (length + this.currentIndex + diff) % length;\n while (\n // don't cycle the elements more than once\n steps &&\n this.elements[nextIndex] &&\n !this.isFocusableElement(this.elements[nextIndex])\n ) {\n nextIndex = (length + nextIndex + diff) % length;\n steps -= 1;\n }\n this.currentIndex = nextIndex;\n }\n\n hostContainsFocus(): void {\n this.host.addEventListener('focusout', this.handleFocusout);\n this.host.addEventListener('keydown', this.handleKeydown);\n this.focused = true;\n }\n\n hostNoLongerContainsFocus(): void {\n this.host.addEventListener('focusin', this.handleFocusin);\n this.host.removeEventListener('focusout', this.handleFocusout);\n this.host.removeEventListener('keydown', this.handleKeydown);\n this.currentIndex = this.focusInIndex;\n this.focused = false;\n }\n\n isRelatedTargetAnElement(event: FocusEvent): boolean {\n const relatedTarget = event.relatedTarget as null | Element;\n return !this.elements.includes(relatedTarget as T);\n }\n\n handleFocusin = (event: FocusEvent): void => {\n if (!this.isEventWithinListenerScope(event)) return;\n if (this.isRelatedTargetAnElement(event)) {\n this.hostContainsFocus();\n }\n const path = event.composedPath() as T[];\n let targetIndex = -1;\n path.find((el) => {\n targetIndex = this.elements.indexOf(el);\n return targetIndex !== -1;\n });\n this.currentIndex = targetIndex > -1 ? targetIndex : this.currentIndex;\n };\n\n handleFocusout = (event: FocusEvent): void => {\n if (this.isRelatedTargetAnElement(event)) {\n this.hostNoLongerContainsFocus();\n }\n };\n\n acceptsEventCode(code: string): boolean {\n if (code === 'End' || code === 'Home') {\n return true;\n }\n switch (this.direction) {\n case 'horizontal':\n return code === 'ArrowLeft' || code === 'ArrowRight';\n case 'vertical':\n return code === 'ArrowUp' || code === 'ArrowDown';\n case 'both':\n case 'grid':\n return code.startsWith('Arrow');\n }\n }\n\n handleKeydown = (event: KeyboardEvent): void => {\n if (!this.acceptsEventCode(event.code) || event.defaultPrevented) {\n return;\n }\n let diff = 0;\n switch (event.code) {\n case 'ArrowRight':\n diff += 1;\n break;\n case 'ArrowDown':\n diff += this.direction === 'grid' ? this.directionLength : 1;\n break;\n case 'ArrowLeft':\n diff -= 1;\n break;\n case 'ArrowUp':\n diff -= this.direction === 'grid' ? this.directionLength : 1;\n break;\n case 'End':\n this.currentIndex = 0;\n diff -= 1;\n break;\n case 'Home':\n this.currentIndex = this.elements.length - 1;\n diff += 1;\n break;\n }\n event.preventDefault();\n if (this.direction === 'grid' && this.currentIndex + diff < 0) {\n this.currentIndex = 0;\n } else if (\n this.direction === 'grid' &&\n this.currentIndex + diff > this.elements.length - 1\n ) {\n this.currentIndex = this.elements.length - 1;\n } else {\n this.setCurrentIndexCircularly(diff);\n }\n // To allow the `focusInIndex` to be calculated with the \"after\" state of the keyboard interaction\n // do `elementEnterAction` _before_ focusing the next element.\n this.elementEnterAction(this.elements[this.currentIndex]);\n this.focus();\n };\n\n manage(): void {\n this.addEventListeners();\n }\n\n unmanage(): void {\n this.removeEventListeners();\n }\n\n addEventListeners(): void {\n this.host.addEventListener('focusin', this.handleFocusin);\n }\n\n removeEventListeners(): void {\n this.host.removeEventListener('focusin', this.handleFocusin);\n this.host.removeEventListener('focusout', this.handleFocusout);\n this.host.removeEventListener('keydown', this.handleKeydown);\n }\n\n hostConnected(): void {\n this.addEventListeners();\n }\n\n hostDisconnected(): void {\n this.removeEventListeners();\n }\n}\n"],
|
|
5
|
-
"mappings": "aAuBA,SAASA,EACLC,EACAC,EACAC,EACC,CACD,OAAI,OAAOF,IAAUC,EACT,IAAMD,EACP,OAAOA,GAAU,WACjBA,EAEJE,CACX,CAEO,aAAM,oBAEb,
|
|
4
|
+
"sourcesContent": ["/*\nCopyright 2020 Adobe. All rights reserved.\nThis file is licensed to you under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License. You may obtain a copy\nof the License at http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under\nthe License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\nOF ANY KIND, either express or implied. See the License for the specific language\ngoverning permissions and limitations under the License.\n*/\nimport type { ReactiveController, ReactiveElement } from 'lit';\n\ntype DirectionTypes = 'horizontal' | 'vertical' | 'both' | 'grid';\nexport type FocusGroupConfig<T> = {\n focusInIndex?: (_elements: T[]) => number;\n direction?: DirectionTypes | (() => DirectionTypes);\n elementEnterAction?: (el: T) => void;\n elements: () => T[];\n isFocusableElement?: (el: T) => boolean;\n listenerScope?: HTMLElement | (() => HTMLElement);\n};\n\nfunction ensureMethod<T, RT>(\n value: T | RT | undefined,\n type: string,\n fallback: T\n): T {\n if (typeof value === type) {\n return (() => value) as T;\n } else if (typeof value === 'function') {\n return value as T;\n }\n return fallback;\n}\n\nexport class FocusGroupController<T extends HTMLElement>\n implements ReactiveController\n{\n protected cachedElements?: T[];\n\n get currentIndex(): number {\n if (this._currentIndex === -1) {\n this._currentIndex = this.focusInIndex;\n }\n return this._currentIndex - this.offset;\n }\n\n set currentIndex(currentIndex) {\n this._currentIndex = currentIndex + this.offset;\n }\n\n private _currentIndex = -1;\n\n get direction(): DirectionTypes {\n return this._direction();\n }\n\n _direction = (): DirectionTypes => 'both';\n\n public directionLength = 5;\n\n elementEnterAction = (_el: T): void => {\n return;\n };\n\n get elements(): T[] {\n if (!this.cachedElements) {\n this.cachedElements = this._elements();\n }\n return this.cachedElements;\n }\n\n private _elements!: () => T[];\n\n protected set focused(focused: boolean) {\n /* c8 ignore next 1 */\n if (focused === this.focused) return;\n this._focused = focused;\n }\n\n protected get focused(): boolean {\n return this._focused;\n }\n\n private _focused = false;\n\n get focusInElement(): T {\n return this.elements[this.focusInIndex];\n }\n\n get focusInIndex(): number {\n return this._focusInIndex(this.elements);\n }\n\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n _focusInIndex = (_elements: T[]): number => 0;\n\n host: ReactiveElement;\n\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n isFocusableElement = (_el: T): boolean => true;\n\n isEventWithinListenerScope(event: Event): boolean {\n if (this._listenerScope() === this.host) return true;\n return event.composedPath().includes(this._listenerScope());\n }\n\n _listenerScope = (): HTMLElement => this.host;\n\n // When elements are virtualized, the delta between the first element\n // and the first rendered element.\n offset = 0;\n\n constructor(\n host: ReactiveElement,\n {\n direction,\n elementEnterAction,\n elements,\n focusInIndex,\n isFocusableElement,\n listenerScope,\n }: FocusGroupConfig<T> = { elements: () => [] }\n ) {\n this.host = host;\n this.host.addController(this);\n this._elements = elements;\n this.isFocusableElement = isFocusableElement || this.isFocusableElement;\n this._direction = ensureMethod<() => DirectionTypes, DirectionTypes>(\n direction,\n 'string',\n this._direction\n );\n this.elementEnterAction = elementEnterAction || this.elementEnterAction;\n this._focusInIndex = ensureMethod<(_elements: T[]) => number, number>(\n focusInIndex,\n 'number',\n this._focusInIndex\n );\n this._listenerScope = ensureMethod<() => HTMLElement, HTMLElement>(\n listenerScope,\n 'object',\n this._listenerScope\n );\n }\n\n update({ elements }: FocusGroupConfig<T> = { elements: () => [] }): void {\n this.unmanage();\n this._elements = elements;\n this.clearElementCache();\n this.manage();\n }\n\n focus(options?: FocusOptions): void {\n let focusElement = this.elements[this.currentIndex];\n if (!focusElement || !this.isFocusableElement(focusElement)) {\n this.setCurrentIndexCircularly(1);\n focusElement = this.elements[this.currentIndex];\n }\n if (focusElement && this.isFocusableElement(focusElement)) {\n focusElement.focus(options);\n }\n }\n\n clearElementCache(offset = 0): void {\n delete this.cachedElements;\n this.offset = offset;\n }\n\n setCurrentIndexCircularly(diff: number): void {\n const { length } = this.elements;\n let steps = length;\n // start at a possibly not 0 index\n let nextIndex = (length + this.currentIndex + diff) % length;\n while (\n // don't cycle the elements more than once\n steps &&\n this.elements[nextIndex] &&\n !this.isFocusableElement(this.elements[nextIndex])\n ) {\n nextIndex = (length + nextIndex + diff) % length;\n steps -= 1;\n }\n this.currentIndex = nextIndex;\n }\n\n hostContainsFocus(): void {\n this.host.addEventListener('focusout', this.handleFocusout);\n this.host.addEventListener('keydown', this.handleKeydown);\n this.focused = true;\n }\n\n hostNoLongerContainsFocus(): void {\n this.host.addEventListener('focusin', this.handleFocusin);\n this.host.removeEventListener('focusout', this.handleFocusout);\n this.host.removeEventListener('keydown', this.handleKeydown);\n this.currentIndex = this.focusInIndex;\n this.focused = false;\n }\n\n isRelatedTargetAnElement(event: FocusEvent): boolean {\n const relatedTarget = event.relatedTarget as null | Element;\n return !this.elements.includes(relatedTarget as T);\n }\n\n handleFocusin = (event: FocusEvent): void => {\n if (!this.isEventWithinListenerScope(event)) return;\n if (this.isRelatedTargetAnElement(event)) {\n this.hostContainsFocus();\n }\n const path = event.composedPath() as T[];\n let targetIndex = -1;\n path.find((el) => {\n targetIndex = this.elements.indexOf(el);\n return targetIndex !== -1;\n });\n this.currentIndex = targetIndex > -1 ? targetIndex : this.currentIndex;\n };\n\n handleFocusout = (event: FocusEvent): void => {\n if (this.isRelatedTargetAnElement(event)) {\n this.hostNoLongerContainsFocus();\n }\n };\n\n acceptsEventCode(code: string): boolean {\n if (code === 'End' || code === 'Home') {\n return true;\n }\n switch (this.direction) {\n case 'horizontal':\n return code === 'ArrowLeft' || code === 'ArrowRight';\n case 'vertical':\n return code === 'ArrowUp' || code === 'ArrowDown';\n case 'both':\n case 'grid':\n return code.startsWith('Arrow');\n }\n }\n\n handleKeydown = (event: KeyboardEvent): void => {\n if (!this.acceptsEventCode(event.code) || event.defaultPrevented) {\n return;\n }\n let diff = 0;\n switch (event.code) {\n case 'ArrowRight':\n diff += 1;\n break;\n case 'ArrowDown':\n diff += this.direction === 'grid' ? this.directionLength : 1;\n break;\n case 'ArrowLeft':\n diff -= 1;\n break;\n case 'ArrowUp':\n diff -= this.direction === 'grid' ? this.directionLength : 1;\n break;\n case 'End':\n this.currentIndex = 0;\n diff -= 1;\n break;\n case 'Home':\n this.currentIndex = this.elements.length - 1;\n diff += 1;\n break;\n }\n event.preventDefault();\n if (this.direction === 'grid' && this.currentIndex + diff < 0) {\n this.currentIndex = 0;\n } else if (\n this.direction === 'grid' &&\n this.currentIndex + diff > this.elements.length - 1\n ) {\n this.currentIndex = this.elements.length - 1;\n } else {\n this.setCurrentIndexCircularly(diff);\n }\n // To allow the `focusInIndex` to be calculated with the \"after\" state of the keyboard interaction\n // do `elementEnterAction` _before_ focusing the next element.\n this.elementEnterAction(this.elements[this.currentIndex]);\n this.focus();\n };\n\n manage(): void {\n this.addEventListeners();\n }\n\n unmanage(): void {\n this.removeEventListeners();\n }\n\n addEventListeners(): void {\n this.host.addEventListener('focusin', this.handleFocusin);\n }\n\n removeEventListeners(): void {\n this.host.removeEventListener('focusin', this.handleFocusin);\n this.host.removeEventListener('focusout', this.handleFocusout);\n this.host.removeEventListener('keydown', this.handleKeydown);\n }\n\n hostConnected(): void {\n this.addEventListeners();\n }\n\n hostDisconnected(): void {\n this.removeEventListeners();\n }\n}\n"],
|
|
5
|
+
"mappings": "aAuBA,SAASA,EACLC,EACAC,EACAC,EACC,CACD,OAAI,OAAOF,IAAUC,EACT,IAAMD,EACP,OAAOA,GAAU,WACjBA,EAEJE,CACX,CAEO,aAAM,oBAEb,CA4EI,YACIC,EACA,CACI,UAAAC,EACA,mBAAAC,EACA,SAAAC,EACA,aAAAC,EACA,mBAAAC,EACA,cAAAC,CACJ,EAAyB,CAAE,SAAU,IAAM,CAAC,CAAE,EAChD,CAxEF,KAAQ,cAAgB,GAMxB,gBAAa,IAAsB,OAEnC,KAAO,gBAAkB,EAEzB,wBAAsBC,GAAiB,CAEvC,EAqBA,KAAQ,SAAW,GAWnB,mBAAiBC,GAA2B,EAK5C,wBAAsBD,GAAoB,GAO1C,oBAAiB,IAAmB,KAAK,KAIzC,YAAS,EA8FT,mBAAiBE,GAA4B,CACzC,GAAI,CAAC,KAAK,2BAA2BA,CAAK,EAAG,OACzC,KAAK,yBAAyBA,CAAK,GACnC,KAAK,kBAAkB,EAE3B,MAAMC,EAAOD,EAAM,aAAa,EAChC,IAAIE,EAAc,GAClBD,EAAK,KAAME,IACPD,EAAc,KAAK,SAAS,QAAQC,CAAE,EAC/BD,IAAgB,GAC1B,EACD,KAAK,aAAeA,EAAc,GAAKA,EAAc,KAAK,YAC9D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,KAAK,KAAK,iBAAiB,WAAY,KAAK,cAAc,EAC1D,KAAK,KAAK,iBAAiB,UAAW,KAAK,aAAa,EACxD,KAAK,QAAU,EACnB,CAEA,2BAAkC,CAC9B,KAAK,KAAK,iBAAiB,UAAW,KAAK,aAAa,EACxD,KAAK,KAAK,oBAAoB,WAAY,KAAK,cAAc,EAC7D,KAAK,KAAK,oBAAoB,UAAW,KAAK,aAAa,EAC3D,KAAK,aAAe,KAAK,aACzB,KAAK,QAAU,EACnB,CAEA,yBAAyBZ,EAA4B,CACjD,MAAMa,EAAgBb,EAAM,cAC5B,MAAO,CAAC,KAAK,SAAS,SAASa,CAAkB,CACrD,CAsBA,iBAAiBC,EAAuB,CACpC,GAAIA,IAAS,OAASA,IAAS,OAC3B,MAAO,GAEX,OAAQ,KAAK,eACJ,aACD,OAAOA,IAAS,aAAeA,IAAS,iBACvC,WACD,OAAOA,IAAS,WAAaA,IAAS,gBACrC,WACA,OACD,OAAOA,EAAK,WAAW,OAAO,EAE1C,CA8CA,QAAe,CACX,KAAK,kBAAkB,CAC3B,CAEA,UAAiB,CACb,KAAK,qBAAqB,CAC9B,CAEA,mBAA0B,CACtB,KAAK,KAAK,iBAAiB,UAAW,KAAK,aAAa,CAC5D,CAEA,sBAA6B,CACzB,KAAK,KAAK,oBAAoB,UAAW,KAAK,aAAa,EAC3D,KAAK,KAAK,oBAAoB,WAAY,KAAK,cAAc,EAC7D,KAAK,KAAK,oBAAoB,UAAW,KAAK,aAAa,CAC/D,CAEA,eAAsB,CAClB,KAAK,kBAAkB,CAC3B,CAEA,kBAAyB,CACrB,KAAK,qBAAqB,CAC9B,CACJ",
|
|
6
6
|
"names": ["ensureMethod", "value", "type", "fallback", "host", "direction", "elementEnterAction", "elements", "focusInIndex", "isFocusableElement", "listenerScope", "_el", "_elements", "event", "path", "targetIndex", "el", "diff", "currentIndex", "focused", "options", "focusElement", "offset", "length", "steps", "nextIndex", "relatedTarget", "code"]
|
|
7
7
|
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
import { html, LitElement } from "lit";
|
|
3
|
+
import { elementUpdated, expect, fixture } from "@open-wc/testing";
|
|
4
|
+
import { ElementResolutionController } from "@spectrum-web-components/reactive-controllers/src/ElementResolution.js";
|
|
5
|
+
describe("Match Media", () => {
|
|
6
|
+
it("responds to media changes", async () => {
|
|
7
|
+
class TestEl extends LitElement {
|
|
8
|
+
}
|
|
9
|
+
if (!customElements.get("test-element-resolution-el")) {
|
|
10
|
+
customElements.define("test-element-resolution-el", TestEl);
|
|
11
|
+
}
|
|
12
|
+
const test = await fixture(
|
|
13
|
+
html`
|
|
14
|
+
<div>
|
|
15
|
+
<test-element-resolution-el></test-element-resolution-el>
|
|
16
|
+
<div class="target" id="one"></div>
|
|
17
|
+
<div class="target" id="two"></div>
|
|
18
|
+
</div>
|
|
19
|
+
`
|
|
20
|
+
);
|
|
21
|
+
const el = test.querySelector("test-element-resolution-el");
|
|
22
|
+
const target1 = test.querySelector("#one");
|
|
23
|
+
const target2 = test.querySelector("#two");
|
|
24
|
+
const controller = new ElementResolutionController(el);
|
|
25
|
+
expect(controller.element).to.be.null;
|
|
26
|
+
controller.selector = ".target";
|
|
27
|
+
await elementUpdated(el);
|
|
28
|
+
expect(controller.element === target1).to.be.true;
|
|
29
|
+
test.insertAdjacentElement("afterbegin", target2);
|
|
30
|
+
await elementUpdated(el);
|
|
31
|
+
expect(controller.element === target2).to.be.true;
|
|
32
|
+
target2.setAttribute("class", "not-target");
|
|
33
|
+
await elementUpdated(el);
|
|
34
|
+
expect(controller.element === target1).to.be.true;
|
|
35
|
+
target2.setAttribute("class", "target");
|
|
36
|
+
await elementUpdated(el);
|
|
37
|
+
expect(controller.element === target2).to.be.true;
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
//# sourceMappingURL=element-resolution.test.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["element-resolution.test.ts"],
|
|
4
|
+
"sourcesContent": ["/*\nCopyright 2022 Adobe. All rights reserved.\nThis file is licensed to you under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License. You may obtain a copy\nof the License at http://www.apache.org/licenses/LICENSE-2.0\n\nUnless required by applicable law or agreed to in writing, software distributed under\nthe License is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS\nOF ANY KIND, either express or implied. See the License for the specific language\ngoverning permissions and limitations under the License.\n*/\n\nimport { html, LitElement } from 'lit';\nimport { elementUpdated, expect, fixture } from '@open-wc/testing';\nimport { ElementResolutionController } from '@spectrum-web-components/reactive-controllers/src/ElementResolution.js';\n\ndescribe('Match Media', () => {\n it('responds to media changes', async () => {\n class TestEl extends LitElement {}\n if (!customElements.get('test-element-resolution-el')) {\n customElements.define('test-element-resolution-el', TestEl);\n }\n const test = await fixture(\n html`\n <div>\n <test-element-resolution-el></test-element-resolution-el>\n <div class=\"target\" id=\"one\"></div>\n <div class=\"target\" id=\"two\"></div>\n </div>\n `\n );\n const el = test.querySelector('test-element-resolution-el') as TestEl;\n const target1 = test.querySelector('#one') as HTMLDivElement;\n const target2 = test.querySelector('#two') as HTMLDivElement;\n const controller = new ElementResolutionController(el as LitElement);\n expect(controller.element).to.be.null;\n controller.selector = '.target';\n await elementUpdated(el);\n expect(controller.element === target1).to.be.true;\n test.insertAdjacentElement('afterbegin', target2);\n await elementUpdated(el);\n expect(controller.element === target2).to.be.true;\n target2.setAttribute('class', 'not-target');\n await elementUpdated(el);\n expect(controller.element === target1).to.be.true;\n target2.setAttribute('class', 'target');\n await elementUpdated(el);\n expect(controller.element === target2).to.be.true;\n });\n});\n"],
|
|
5
|
+
"mappings": ";AAYA,SAAS,MAAM,kBAAkB;AACjC,SAAS,gBAAgB,QAAQ,eAAe;AAChD,SAAS,mCAAmC;AAE5C,SAAS,eAAe,MAAM;AAC1B,KAAG,6BAA6B,YAAY;AACxC,UAAM,eAAe,WAAW;AAAA,IAAC;AACjC,QAAI,CAAC,eAAe,IAAI,4BAA4B,GAAG;AACnD,qBAAe,OAAO,8BAA8B,MAAM;AAAA,IAC9D;AACA,UAAM,OAAO,MAAM;AAAA,MACf;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,IAOJ;AACA,UAAM,KAAK,KAAK,cAAc,4BAA4B;AAC1D,UAAM,UAAU,KAAK,cAAc,MAAM;AACzC,UAAM,UAAU,KAAK,cAAc,MAAM;AACzC,UAAM,aAAa,IAAI,4BAA4B,EAAgB;AACnE,WAAO,WAAW,OAAO,EAAE,GAAG,GAAG;AACjC,eAAW,WAAW;AACtB,UAAM,eAAe,EAAE;AACvB,WAAO,WAAW,YAAY,OAAO,EAAE,GAAG,GAAG;AAC7C,SAAK,sBAAsB,cAAc,OAAO;AAChD,UAAM,eAAe,EAAE;AACvB,WAAO,WAAW,YAAY,OAAO,EAAE,GAAG,GAAG;AAC7C,YAAQ,aAAa,SAAS,YAAY;AAC1C,UAAM,eAAe,EAAE;AACvB,WAAO,WAAW,YAAY,OAAO,EAAE,GAAG,GAAG;AAC7C,YAAQ,aAAa,SAAS,QAAQ;AACtC,UAAM,eAAe,EAAE;AACvB,WAAO,WAAW,YAAY,OAAO,EAAE,GAAG,GAAG;AAAA,EACjD,CAAC;AACL,CAAC;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|