-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Vertical two-up #100
Vertical two-up #100
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,15 @@ | ||
import * as styles from './styles.css'; | ||
import { PointerTracker, Pointer } from '../../../../lib/PointerTracker'; | ||
|
||
const legacyClipCompat = 'legacy-clip-compat'; | ||
const legacyClipCompatAttr = 'legacy-clip-compat'; | ||
const verticalAttr = 'vertical'; | ||
|
||
/** | ||
* A split view that the user can adjust. The first child becomes | ||
* the left-hand side, and the second child becomes the right-hand side. | ||
*/ | ||
export default class TwoUp extends HTMLElement { | ||
static get observedAttributes () { return [legacyClipCompat]; } | ||
static get observedAttributes() { return [verticalAttr]; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I had |
||
|
||
private readonly _handle = document.createElement('div'); | ||
/** | ||
|
@@ -52,39 +53,64 @@ export default class TwoUp extends HTMLElement { | |
}); | ||
} | ||
|
||
connectedCallback () { | ||
connectedCallback() { | ||
this._childrenChange(); | ||
if (!this._everConnected) { | ||
// Set the initial position of the handle. | ||
requestAnimationFrame(() => { | ||
const bounds = this.getBoundingClientRect(); | ||
this._position = bounds.width / 2; | ||
this._setPosition(); | ||
}); | ||
this._resetPosition(); | ||
this._everConnected = true; | ||
} | ||
} | ||
|
||
attributeChangedCallback(name: string) { | ||
if (name === verticalAttr) { | ||
this._resetPosition(); | ||
} | ||
} | ||
|
||
private _resetPosition() { | ||
// Set the initial position of the handle. | ||
requestAnimationFrame(() => { | ||
const bounds = this.getBoundingClientRect(); | ||
this._position = (this.vertical ? bounds.height : bounds.width) / 2; | ||
this._setPosition(); | ||
}); | ||
} | ||
|
||
/** | ||
* If true, this element works in browsers that don't support clip-path (Edge). | ||
* However, this means you'll have to set the height of this element manually. | ||
*/ | ||
get noClipPathCompat () { | ||
return this.hasAttribute(legacyClipCompat); | ||
get legacyClipCompat() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I renamed the attr but forgot to rename the prop. |
||
return this.hasAttribute(legacyClipCompatAttr); | ||
} | ||
|
||
set noClipPathCompat (val: boolean) { | ||
set legacyClipCompat(val: boolean) { | ||
if (val) { | ||
this.setAttribute(legacyClipCompat, ''); | ||
this.setAttribute(legacyClipCompatAttr, ''); | ||
} else { | ||
this.removeAttribute(legacyClipCompat); | ||
this.removeAttribute(legacyClipCompatAttr); | ||
} | ||
} | ||
|
||
/** | ||
* Split vertically rather than horizontally. | ||
*/ | ||
get vertical() { | ||
return this.hasAttribute(verticalAttr); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's repetition here, but I'm not sure if there's a typescript-friendly way to solve it. I'd usually transform the attr names to property names then generate the getters/setters automatically. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could write a decorator for tying attrs with props. Not sure if it’s worth it (yet). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we feel we need it, we should talk to the polymer folks. Pretty sure they'll have thought about this before. |
||
|
||
set vertical(val: boolean) { | ||
if (val) { | ||
this.setAttribute(verticalAttr, ''); | ||
} else { | ||
this.removeAttribute(verticalAttr); | ||
} | ||
} | ||
|
||
/** | ||
* Called when element's child list changes | ||
*/ | ||
private _childrenChange () { | ||
private _childrenChange() { | ||
// Ensure the handle is the last child. | ||
// The CSS depends on this. | ||
if (this.lastElementChild !== this._handle) { | ||
|
@@ -95,15 +121,20 @@ export default class TwoUp extends HTMLElement { | |
/** | ||
* Called when a pointer moves. | ||
*/ | ||
private _pointerChange (startPoint: Pointer, currentPoint: Pointer) { | ||
private _pointerChange(startPoint: Pointer, currentPoint: Pointer) { | ||
const pointAxis = this.vertical ? 'clientY' : 'clientX'; | ||
const dimensionAxis = this.vertical ? 'height' : 'width'; | ||
const bounds = this.getBoundingClientRect(); | ||
this._position = this._positionOnPointerStart + (currentPoint.clientX - startPoint.clientX); | ||
|
||
this._position = this._positionOnPointerStart + | ||
(currentPoint[pointAxis] - startPoint[pointAxis]); | ||
|
||
// Clamp position to element bounds. | ||
this._position = Math.max(0, Math.min(this._position, bounds.width)); | ||
this._position = Math.max(0, Math.min(this._position, bounds[dimensionAxis])); | ||
this._setPosition(); | ||
} | ||
|
||
private _setPosition () { | ||
private _setPosition() { | ||
this.style.setProperty('--split-point', `${this._position}px`); | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this meant to be like the disabled attribute, where by presence indicates the state? I was wondering why this wasn't just an
orientation
attribute with a type that has a default of horizontal, but the option of 'vertical'