-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add user.type and refactor user API (#48)
- Loading branch information
Showing
14 changed files
with
864 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'test-mule': minor | ||
--- | ||
|
||
- Add user.type method | ||
- Add actionability checks: visible and attached |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
export { printElement } from '../serialize'; | ||
|
||
export const assertAttached = (el: Element) => { | ||
if (!el.isConnected) { | ||
throw error`Cannot perform action on element that is not attached to the DOM: | ||
${el}`; | ||
} | ||
}; | ||
|
||
// Element is visible if all: | ||
// - it is attached to the DOM | ||
// - it has a rendered size (its rendered width and height are not zero) | ||
// - Computed opacity (product of opacity of ancestors) is non-zero | ||
// - is not display: none or visibility: hidden | ||
export const assertVisible = (el: Element) => { | ||
assertAttached(el); | ||
|
||
// getComputedStyle allows inherited properties to be seen correctly | ||
const style = getComputedStyle(el); | ||
|
||
if (style.visibility === 'hidden') { | ||
throw error`Cannot perform action on element that is not visible (it has visibility:hidden): | ||
${el}`; | ||
} | ||
|
||
// The opacity of a parent element affects the rendering of a child element, | ||
// but the opacity property is not inherited, so this computes the rendered opacity | ||
// by walking up the tree and multiplying the opacities. | ||
let opacity = Number(style.opacity); | ||
let opacityEl: Element | null = el; | ||
while (opacity && (opacityEl = opacityEl.parentElement)) { | ||
opacity *= (getComputedStyle(opacityEl).opacity as any) as number; | ||
} | ||
|
||
if (opacity < 0.05) { | ||
throw error`Cannot perform action on element that is not visible (it is near zero opacity): | ||
${el}`; | ||
} | ||
|
||
const rect = el.getBoundingClientRect(); | ||
// handles: rendered width is zero or rendered height is zero or display:none | ||
if (rect.width * rect.height === 0) { | ||
throw error`Cannot perform action on element that is not visible (it was not rendered or has a size of zero): | ||
${el}`; | ||
} | ||
}; | ||
|
||
// this is used to generate the arrays that are used | ||
// to produce messages with live elements in the browser, | ||
// and stringified elements in node | ||
// example usage: | ||
// error`something bad happened: ${el}` | ||
// returns { error: ['something bad happened', el]} | ||
export const error = ( | ||
literals: TemplateStringsArray, | ||
...placeholders: Element[] | ||
) => { | ||
return { | ||
error: literals.reduce((acc, val, i) => { | ||
if (i !== 0) acc.push(placeholders[i - 1]); | ||
if (val !== '') acc.push(val); | ||
return acc; | ||
}, [] as (string | Element)[]), | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import babel from '@rollup/plugin-babel'; | ||
import nodeResolve from '@rollup/plugin-node-resolve'; | ||
import { terser } from 'rollup-plugin-terser'; | ||
|
||
const extensions = ['.js', '.jsx', '.es6', '.es', '.mjs', '.ts', '.tsx']; | ||
|
||
/** @type {import('rollup').RollupOptions} */ | ||
const config = { | ||
input: ['src/user-util/index.ts'], | ||
plugins: [ | ||
babel({ babelHelpers: 'bundled', extensions }), | ||
nodeResolve({ extensions }), | ||
terser({ ecma: 2019 }), | ||
], | ||
output: { file: 'dist/user-util.js' }, | ||
}; | ||
|
||
export default config; |
Oops, something went wrong.