Skip to content

Instantly share code, notes, and snippets.

@skovy
Created December 9, 2020 19:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skovy/cc82245d99de74190b2e161c9becc27c to your computer and use it in GitHub Desktop.
Save skovy/cc82245d99de74190b2e161c9becc27c to your computer and use it in GitHub Desktop.
const HIGHLIGHT_WIDTH = 2;
const HIGHLIGHT_SELECTOR = '[data-component]';
function createHighlighterForElement(component: HTMLElement) {
const highlighter = document.createElement('div');
const { x, y, width, height } = component.getBoundingClientRect();
highlighter.style.cssText = `
top: ${y + window.scrollY - HIGHLIGHT_WIDTH}px;
left: ${x - HIGHLIGHT_WIDTH}px;
height: ${height}px;
width: ${width}px;
border: ${HIGHLIGHT_WIDTH}px solid red;
position: absolute;
`;
return highlighter;
}
function highlightComponents() {
const components = document.querySelectorAll<HTMLElement>(HIGHLIGHT_SELECTOR);
components.forEach((component) => {
const highlighter = createHighlighterForElement(component);
document.body.appendChild(highlighter);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment