segmentio / chrome-sidebar

Easiest way to embed an iframe as a chrome extension
https://open.segment.com
227 stars 58 forks source link

Expose way of detecting if sidebar is open on `Frame` #9

Open breadadams opened 4 years ago

breadadams commented 4 years ago

Currently there are 2 static methods on the Frame class, isReady() and toggle(). These are useful, but for our use case we also need a way of detecting if the sidebar is open or minimized, for example isOpen().

This being because we have some code that runs that opens the sidebar. If isReady() === true we run toggle(), however we don't want to toggle if it the user has the sidebar open already.

if (Frame.isReady()) {
  if (!Frame.isOpen()) {
    Frame.toggle()
  }
} else {
  // create Frame
}

Currently to achieve this I've got some hacky code:

if (Frame.isReady()) {
  // only toggle frame if it's not open
  const iframeEl = document.getElementById("iframe-id");

  if (iframeEl) {
    const parentStyles = window.getComputedStyle(iframeEl.parentNode);
    const parentTransform = parentStyles.transform;
    const isMinimized = parentTransform !== "matrix(1, 0, 0, 1, 0, 0)";

    if (isMinimized) {
      Frame.toggle();
    }
  }
} else {
  const root = document.createElement("div");
  document.body.appendChild(root);

  ReactDOM.render(<App />, root);
}
breadadams commented 4 years ago

Would you accept a PR with this addition @fouad?