Skip to content

Commit f528bdf

Browse files
addyosmaniargyleink
authored andcommitted
Adds z-index visualization plugin (GoogleChromeLabs#418)
* Adds z-index visualization plugin This change introduces a `z-index` visualization plugin. The idea is to render a box with the `z-index` value for each child, to help developers debug index stacking. Each box is given a random color to help distinguish what you're seeing. * Update implem. to match Adam's visbug native tweaks
1 parent e2ef023 commit f528bdf

4 files changed

Lines changed: 37 additions & 3 deletions

File tree

app/plugins/_dynamic-registery.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const entries = [
44
'pesticide.js',
55
'construct.js',
66
'construct.debug.js',
7+
'zindex.js',
78
]
89

910
const PluginRegistry = new Map()

app/plugins/_registry.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ import { commands as revenge_commands, default as RevengePlugin } from './reveng
1010
import { commands as tota11y_commands, default as Tota11yPlugin } from './tota11y'
1111
import { commands as shuffle_commands, default as ShufflePlugin } from './shuffle'
1212
import { commands as colorblind_commands, default as ColorblindPlugin } from './colorblind'
13+
import { commands as zindex_commands, default as ZIndexPlugin } from './zindex'
1314

1415
const commandsToHash = (plugin_commands, plugin_fn) =>
1516
plugin_commands.reduce((commands, command) =>
16-
Object.assign(commands, {[`/${command}`]:plugin_fn})
17-
, {})
17+
Object.assign(commands, { [`/${command}`]: plugin_fn })
18+
, {})
1819

1920
export const PluginRegistry = new Map(Object.entries({
2021
...commandsToHash(blank_page_commands, BlankPagePlugin),
@@ -29,6 +30,7 @@ export const PluginRegistry = new Map(Object.entries({
2930
...commandsToHash(tota11y_commands, Tota11yPlugin),
3031
...commandsToHash(shuffle_commands, ShufflePlugin),
3132
...commandsToHash(colorblind_commands, ColorblindPlugin),
33+
...commandsToHash(zindex_commands, ZIndexPlugin),
3234
}))
3335

3436
export const PluginHints = [
@@ -43,5 +45,6 @@ export const PluginHints = [
4345
revenge_commands[0],
4446
tota11y_commands[0],
4547
shuffle_commands[0],
48+
zindex_commands[0],
4649
...colorblind_commands,
4750
].map(command => `/${command}`)

app/plugins/blank-page.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export const commands = [
44
'clear canvas',
55
]
66

7-
export default function() {
7+
export default function () {
88
document
99
.querySelectorAll('body > *:not(vis-bug):not(script)')
1010
.forEach(node => node.remove())

app/plugins/zindex.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
export const commands = [
2+
'zindex',
3+
'z-index'
4+
]
5+
6+
export default function () {
7+
// Fun prior art https://gist.github.com/paulirish/211209
8+
Array.from(document.querySelectorAll('*'))
9+
.filter(el => el.computedStyleMap().get('z-index').value !== 'auto')
10+
.filter(el => el.nodeName !== 'VIS-BUG')
11+
.forEach(el => {
12+
const color = `#${Math.floor(Math.random() * 16777215).toString(16)}`
13+
const zindex = el.computedStyleMap().get('z-index').value
14+
15+
const label = document.createElement('visbug-label')
16+
17+
label.text = `z-index: ${zindex}`
18+
label.position = {
19+
boundingRect: el.getBoundingClientRect()
20+
}
21+
label.style.setProperty(`--label-bg`, color)
22+
23+
const overlay = document.createElement('visbug-hover')
24+
overlay.position = { el }
25+
overlay.style.setProperty(`--hover-stroke`, color)
26+
27+
document.body.appendChild(label)
28+
document.body.appendChild(overlay)
29+
})
30+
}

0 commit comments

Comments
 (0)