Skip to content

Commit

Permalink
Adding show-when-local-storage attributes. Resolves #5.
Browse files Browse the repository at this point in the history
  • Loading branch information
joeldenning committed Jul 19, 2019
1 parent 538fd2d commit 338720c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,19 @@ You use the import-map-overrides UI via web components. This means you just need
You have three options for the UI, depending on how much you want to customize the UI:

```html
<!-- The full UI, including the omnipresent "trigger" button that pops up the UI -->
<import-map-overrides-full></import-map-overrides-full>
<!--
The full UI, including the omnipresent "trigger" button that pops up the UI.
If you omit the show-when-local-storage attribute, the yellow/orange rectangle always shows.
When you have the show-when-local-storage attribute, the yellow/orange rectangle will only show
when the user has set a local storage value of "true" for the key specified.
For example, in the below example you must run the follow command in order to see the overrides "trigger" rectangle.
localStorage.setItem('overrides-ui', true);
-->
<import-map-overrides-full
show-when-local-storage="overrides-ui"
></import-map-overrides-full>

<!-- Alternatively, just the black popup itself -->
<import-map-overrides-popup></import-map-overrides-popup>
Expand Down
20 changes: 17 additions & 3 deletions src/ui/custom-elements.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import List from "./list/list.component";
if (window.customElements) {
window.customElements.define(
"import-map-overrides-full",
preactCustomElement(FullUI)
preactCustomElement(FullUI, ["show-when-local-storage"])
);
window.customElements.define(
"import-map-overrides-popup",
Expand All @@ -19,13 +19,27 @@ if (window.customElements) {
);
}

function preactCustomElement(Comp) {
function preactCustomElement(Comp, observedAttributes = []) {
return class PreactCustomElement extends HTMLElement {
connectedCallback() {
render(h(Comp, this), this);
this.renderWithPreact();
}
disconnectedCallback() {
render(null, this);
this.renderedEl = null;
}
static get observedAttributes() {
return observedAttributes;
}
attributeChangedCallback() {
this.renderWithPreact();
}
renderWithPreact() {
this.renderedEl = render(
h(Comp, { customElement: this }),
this,
this.renderedEl
);
}
};
}
10 changes: 10 additions & 0 deletions src/ui/full-ui.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ export default class FullUI extends Component {
showingPopup: false
};
render(props, state) {
const shouldShow =
!props.customElement.hasAttribute("show-when-local-storage") ||
localStorage.getItem(
props.customElement.getAttribute("show-when-local-storage")
) === "true";

if (!shouldShow) {
return null;
}

const atLeastOneOverride =
Object.keys(window.importMapOverrides.getOverrideMap().imports).length >
0;
Expand Down

0 comments on commit 338720c

Please sign in to comment.