Skip to content

Commit 6644909

Browse files
KilianAdam Argyle
andauthored
First step at fixed label and handle support (#441)
* first step at fixed label and handle support * Update app/components/selection/label.element.js Co-Authored-By: Adam Argyle <[email protected]>
1 parent ffe463c commit 6644909

9 files changed

Lines changed: 37 additions & 18 deletions

File tree

app/components/selection/handles.element.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
@import "../_variables.css";
22

33
:host > svg {
4-
position: absolute;
4+
position: var(--position, 'absolute');
55
top: var(--top);
66
left: var(--left);
77
overflow: visible;

app/components/selection/handles.element.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import $ from 'blingblingjs'
22
import { HandleStyles } from '../styles.store'
3+
import { isFixed } from '../../utilities/';
34

45
export class Handles extends HTMLElement {
56

@@ -13,7 +14,7 @@ export class Handles extends HTMLElement {
1314
this.$shadow.adoptedStyleSheets = this.styles
1415
window.addEventListener('resize', this.on_resize.bind(this))
1516
}
16-
17+
1718
disconnectedCallback() {
1819
window.removeEventListener('resize', this.on_resize)
1920
}
@@ -28,12 +29,13 @@ export class Handles extends HTMLElement {
2829
this.position = {
2930
node_label_id,
3031
el: source_el,
32+
isFixed: isFixed(source_el),
3133
}
3234
})
3335
}
3436

3537
set position({el, node_label_id}) {
36-
this.$shadow.innerHTML = this.render(el.getBoundingClientRect(), node_label_id)
38+
this.$shadow.innerHTML = this.render(el.getBoundingClientRect(), node_label_id, isFixed(el))
3739

3840
if (this._backdrop) {
3941
this.backdrop = {
@@ -53,11 +55,12 @@ export class Handles extends HTMLElement {
5355
: this.$shadow.appendChild(bd.element)
5456
}
5557

56-
render({ x, y, width, height, top, left }, node_label_id) {
58+
render({ x, y, width, height, top, left }, node_label_id, isFixed) {
5759
this.$shadow.host.setAttribute('data-label-id', node_label_id)
5860

59-
this.style.setProperty('--top', `${top + window.scrollY}px`)
61+
this.style.setProperty('--top', `${top + (isFixed ? 0 : window.scrollY)}px`)
6062
this.style.setProperty('--left', `${left}px`)
63+
this.style.setProperty('--position', isFixed ? 'fixed' : 'absolute')
6164

6265
return `
6366
<svg

app/components/selection/hover.element.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ export class Hover extends Handles {
88
this.styles = [HandleStyles, HoverStyles]
99
}
1010

11-
render({ width, height, top, left }) {
12-
this.style.setProperty('--top', `${top + window.scrollY}px`)
11+
render({ width, height, top, left }, node_label_id, isFixed) {
12+
this.style.setProperty('--top', `${top + (isFixed ? 0 : window.scrollY)}px`)
1313
this.style.setProperty('--left', `${left}px`)
14+
this.style.setProperty('--position', isFixed ? 'fixed' : 'absolute')
1415

1516
return `
1617
<svg width="${width}" height="${height}">

app/components/selection/label.element.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
:host {
44
font-size: 16px;
5-
5+
--position: absolute;
66
--top: 0;
77
--left: 0;
88
--max-width: 0;
99
}
1010

1111
:host > span {
12-
position: absolute;
12+
position: var(--position);
1313
top: var(--top);
1414
left: var(--left);
1515
max-width: var(--max-width);

app/components/selection/label.element.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import $ from 'blingblingjs'
22
import { LabelStyles } from '../styles.store'
3+
import { isFixed } from '../../utilities/';
34

45
export class Label extends HTMLElement {
56

@@ -29,6 +30,7 @@ export class Label extends HTMLElement {
2930
this.position = {
3031
node_label_id,
3132
boundingRect: source_el.getBoundingClientRect(),
33+
isFixed: isFixed(source_el),
3234
}
3335
})
3436
}
@@ -47,15 +49,16 @@ export class Label extends HTMLElement {
4749
this._text = content
4850
}
4951

50-
set position({boundingRect, node_label_id}) {
52+
set position({boundingRect, node_label_id, isFixed}) {
5153
this.$shadow.innerHTML = this.render(node_label_id)
52-
this.update = boundingRect
54+
this.update = {boundingRect, isFixed}
5355
}
5456

55-
set update({x,y,width}) {
56-
this.style.setProperty('--top', `${y + window.scrollY}px`)
57-
this.style.setProperty('--left', `${x - 1}px`)
58-
this.style.setProperty('--max-width', `${width + (window.innerWidth - x - width - 20)}px`)
57+
set update({boundingRect, isFixed}) {
58+
this.style.setProperty('--top', `${boundingRect.y + (isFixed ? 0 : window.scrollY)}px`)
59+
this.style.setProperty('--left', `${boundingRect.x - 1}px`)
60+
this.style.setProperty('--max-width', `${boundingRect.width + (window.innerWidth - boundingRect.x - boundingRect.width - 20)}px`)
61+
this.style.setProperty('--position', isFixed ? 'fixed' : 'absolute');
5962
}
6063

6164
render(node_label_id) {

app/features/selectable.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
metaKey, htmlStringToDom, createClassname, camelToDash,
1717
isOffBounds, getStyles, deepElementFromPoint, getShadowValues,
1818
isSelectorValid, findNearestChildElement, findNearestParentElement,
19-
getTextShadowValues
19+
getTextShadowValues, isFixed,
2020
} from '../utilities/'
2121

2222
export function Selectable(visbug) {
@@ -584,7 +584,7 @@ export function Selectable(visbug) {
584584
}
585585

586586
const setLabel = (el, label) =>
587-
label.update = el.getBoundingClientRect()
587+
label.update = {boundingRect: el.getBoundingClientRect(), isFixed: isFixed(el)}
588588

589589
const createLabel = ({el, id, template}) => {
590590
if (!labels[id]) {
@@ -594,6 +594,7 @@ export function Selectable(visbug) {
594594
label.position = {
595595
boundingRect: el.getBoundingClientRect(),
596596
node_label_id: id,
597+
isFixed: isFixed(el),
597598
}
598599

599600
document.body.appendChild(label)

app/plugins/zindex.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { isFixed } from '../utilities/';
2+
13
export const commands = [
24
'zindex',
35
'z-index'
@@ -16,13 +18,15 @@ export default function () {
1618

1719
label.text = `z-index: ${zindex}`
1820
label.position = {
19-
boundingRect: el.getBoundingClientRect()
21+
boundingRect: el.getBoundingClientRect(),
22+
isFixed: isFixed(el),
2023
}
2124
label.style.setProperty(`--label-bg`, color)
2225

2326
const overlay = document.createElement('visbug-hover')
2427
overlay.position = { el }
2528
overlay.style.setProperty(`--hover-stroke`, color)
29+
overlay.style.setProperty(`--position`, isFixed(el) ? 'fixed' : 'absolute')
2630

2731
document.body.appendChild(label)
2832
document.body.appendChild(overlay)

app/utilities/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ export * from './accessibility'
33
export * from './common'
44
export * from './strings'
55
export * from './window'
6+
export * from './isFixed'

app/utilities/isFixed.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export const isFixed = elem => {
2+
do {
3+
if (window.getComputedStyle(elem).position == 'fixed') return true;
4+
} while (elem = elem.offsetParent);
5+
return false;
6+
}

0 commit comments

Comments
 (0)