Skip to content
17 changes: 16 additions & 1 deletion packages/react-guides/src/react-guides/Guides.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export default class Guides extends React.PureComponent<GuidesProps, GuidesState
onDragEnd: () => {},
displayDragPos: false,
dragPosFormat: v => v,
defaultGuides: [],
showGuides: true,
};
public state: GuidesState = {
guides: [],
Expand Down Expand Up @@ -82,11 +84,12 @@ export default class Guides extends React.PureComponent<GuidesProps, GuidesState
</GuidesElement>;
}
public renderGuides() {
const { type, zoom } = this.props as Required<GuidesProps>;
const { type, zoom, showGuides } = this.props as Required<GuidesProps>;
const translateName = type === "horizontal" ? "translateY" : "translateX";
const guides = this.state.guides;

this.guideElements = [];
if(showGuides){
return guides.map((pos, i) => {
return (<div className={prefix("guide", type)}
ref={refs(this, "guideElements", i)}
Expand All @@ -97,6 +100,9 @@ export default class Guides extends React.PureComponent<GuidesProps, GuidesState
transform: `${translateName}(${pos * zoom}px)`,
}}></div>);
});
}else{
return (<div></div>)
}
}
public componentDidMount() {
this.dragger = new Dragger(
Expand All @@ -120,10 +126,19 @@ export default class Guides extends React.PureComponent<GuidesProps, GuidesState
dragend: this.onDragEnd,
},
);
this.setState({guides: this.props.defaultGuides || []}); // pass array of guides on mount data to create gridlines or something like that in ui
}
public componentWillUnmount() {
this.dragger.unset();
}
public componentDidUpdate(prevProps: any) {
if (prevProps.defaultGuides !== this.props.defaultGuides) {
//to dynamically update guides from code rather than dragging guidelines
this.setState({guides: this.props.defaultGuides || []},()=>{
this.renderGuides();
});
}
}
/**
* Load the current guidelines.
* @memberof Guides
Expand Down
3 changes: 3 additions & 0 deletions packages/react-guides/src/react-guides/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ export interface GuideOptions extends RulerProps {
displayDragPos?: boolean;
cspNonce?: string;
dragPosFormat?: (value: number) => string | number;
defaultGuides? :number[];
showGuides?: boolean;

}

/**
Expand Down