Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Commit

Permalink
fix: preview scrolling hangs (fixes #316)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheReincarnator authored and lkuechler committed Apr 4, 2018
1 parent 629e2c0 commit bce364c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 21 deletions.
26 changes: 11 additions & 15 deletions src/styleguide/renderer/highlight-area.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export interface HighlightAreaProps {
}

export class HighlightArea {
private pageElementId?: string;

@observable
private props: HighlightAreaProps = {
bottom: 0,
Expand All @@ -28,11 +30,17 @@ export class HighlightArea {

public hide(): void {
this.props.opacity = 0;
this.pageElementId = undefined;
}

public show(element: Element): void {
public show(element: Element, pageElementId: string): void {
if (this.pageElementId === pageElementId) {
return;
}
this.pageElementId = pageElementId;

const clientRect: ClientRect = element.getBoundingClientRect();
const newProps: HighlightAreaProps = {
this.props = {
bottom: clientRect.bottom,
height: clientRect.height,
left: clientRect.left + window.scrollX,
Expand All @@ -42,19 +50,7 @@ export class HighlightArea {
width: clientRect.width
};

if (
this.props.top === newProps.top &&
this.props.right === newProps.right &&
this.props.bottom === newProps.bottom &&
this.props.left === newProps.left &&
this.props.height === newProps.height &&
this.props.width === newProps.width
) {
return;
}

this.props = newProps;

console.log('Scrolled into view');
element.scrollIntoView({
behavior: 'smooth',
block: 'center',
Expand Down
14 changes: 8 additions & 6 deletions src/styleguide/renderer/react/preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,7 @@ class Preview extends React.Component<PreviewProps> {
}

public componentDidUpdate(prevProps: PreviewProps): void {
if (this.props.selectedElementId) {
this.triggerHighlight();
}
this.triggerHighlight();
}

private createAssetComponent(pageElement: PageElement): JSX.Element {
Expand Down Expand Up @@ -230,9 +228,13 @@ class Preview extends React.Component<PreviewProps> {
}

private triggerHighlight(): void {
const domNode = this.patternWrapperRef && ReactDOM.findDOMNode(this.patternWrapperRef);
if (domNode) {
this.highlightArea.show(domNode);
if (this.props.selectedElementId) {
const domNode = this.patternWrapperRef && ReactDOM.findDOMNode(this.patternWrapperRef);
if (domNode) {
this.highlightArea.show(domNode, this.props.selectedElementId);
}
} else {
this.highlightArea.hide();
}
}
}
Expand Down

0 comments on commit bce364c

Please sign in to comment.