Skip to content

Commit

Permalink
Updated web components
Browse files Browse the repository at this point in the history
  • Loading branch information
davenquinn committed Sep 17, 2024
1 parent 8eb36d7 commit 38671da
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 20 deletions.
4 changes: 2 additions & 2 deletions packages/map-interface/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@macrostrat/map-interface",
"version": "1.0.0",
"version": "1.1.0",
"description": "Map interface for Macrostrat",
"main": "dist/index.cjs",
"module": "dist/index.js",
Expand All @@ -11,7 +11,7 @@
"@macrostrat/hyper": "^2.2.1",
"@macrostrat/mapbox-react": "workspace:*",
"@macrostrat/mapbox-utils": "workspace:*",
"@macrostrat/ui-components": "workspace:*",
"@macrostrat/ui-components": "workspace:^4.1.0",
"@mapbox/tilebelt": "^1.0.2",
"@turf/bbox": "^6.5.0",
"chroma-js": "^2.4.2",
Expand Down
3 changes: 3 additions & 0 deletions packages/map-interface/src/controls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
useMapStatus,
} from "@macrostrat/mapbox-react";
import { ScaleControl as BaseScaleControl } from "mapbox-gl";
import { DevToolsButtonSlot } from "@macrostrat/ui-components";

const h = hyper.styled(styles);

Expand Down Expand Up @@ -55,6 +56,8 @@ export function MapBottomControls({ children }) {
h(CompassControl, { className: "compass-control" }),
h(GlobeControl, { className: "globe-control" }),
h(GeolocationControl, { className: "geolocation-control" }),
// If we have global development tools enabled, show the button
h(DevToolsButtonSlot, { className: "map-control" }),
children,
]);
}
33 changes: 20 additions & 13 deletions packages/map-interface/src/main.module.sass
Original file line number Diff line number Diff line change
Expand Up @@ -275,33 +275,40 @@
flex-direction: row
justify-content: right
margin-bottom: 0
gap: 0.5em

:global(.map-control)
&>:global(.bp5-button)
padding: 0
transform: translate(-3.5px, -3.5px)
width: 22px !important

&>*
margin-left: 0.5em

.map-controls :global(.mapbox-control),
.map-controls :global(.map-control-wrapper),
.map-controls .map-control
min-width: 22px
min-height: 22px
.map-controls :global(.map-control)
max-height: 22px
height: 22px
border-radius: 4px
background-color: var(--panel-background-color)
box-shadow: 0 0 0 1px var(--card-shadow-color)

.map-controls :global(.mapbox-control) button,
.map-controls :global(.map-control-wrapper) button,
.map-controls .map-control button
width: 22px
.map-controls :global(.map-control) button
max-height: 22px
height: 22px
width: 22px
max-width: 22px
background-position: center center
padding: 0
background-color: var(--panel-background-color)
color: var(--text-color)
//background-color: var(--panel-background-color)
//color: var(--text-color)
.map-controls :global(.mapbox-control) button:hover,
.map-controls :global(.map-control-wrapper) button:hover,
.map-controls .map-control button:hover
background-color: var(--panel-background-color) !important
//.map-controls :global(.mapbox-control) button:hover,
//.map-controls :global(.map-control-wrapper) button:hover,
//.map-controls .map-control button:hover
// background-color: var(--panel-background-color) !important
.map-controls .map-scale-control
background: none
Expand Down
5 changes: 3 additions & 2 deletions packages/ui-components/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@macrostrat/ui-components",
"version": "4.0.0",
"version": "4.1.0",
"description": "UI components for React and blueprint.js",
"repository": "https://github.com/UW-Macrostrat/web-components.git",
"main": "dist/ui-components.cjs",
Expand Down Expand Up @@ -46,7 +46,8 @@
"ui-box": "^5.4.1",
"underscore": "^1.9.1",
"use-async-effect": "^2.2.1",
"use-element-dimensions": "^2.1.3"
"use-element-dimensions": "^2.1.3",
"zustand": "^5.0.0-rc.2"
},
"devDependencies": {
"@babel/core": "^7.16.12",
Expand Down
21 changes: 21 additions & 0 deletions packages/ui-components/src/dev-panel/_inner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Dialog } from "@blueprintjs/core";
import hyper from "@macrostrat/hyper";
import styles from "./page-admin.module.sass";

const h = hyper.styled(styles);

export function PageAdminInner({ isOpen, setIsOpen, children }) {
return h([
h(
Dialog,
{
isOpen,
onClose: () => setIsOpen(false),
title: "Page info",
className: "page-admin",
},
h("div.dialog-content.bp5-dialog-content", children)
),
h("span.__render_alarm__"),
]);
}
100 changes: 100 additions & 0 deletions packages/ui-components/src/dev-panel/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { Button } from "@blueprintjs/core";
import { useEffect } from "react";
import hyper from "@macrostrat/hyper";
import loadable from "@loadable/component";
import { create } from "zustand";
import styles from "./page-admin.module.sass";
import classNames from "classnames";

const h = hyper.styled(styles);

const useStore: any = create((set) => {
return {
isOpen: false,
isSystemEnabled: false,
setIsOpen(isOpen) {
set({ isOpen });
},
toggle() {
set((state) => ({ isOpen: !state.isOpen }));
},
toggleButton() {
set((state) => {
let isOpen = state.isSystemEnabled ? false : state.isOpen;
return { isSystemEnabled: !state.isSystemEnabled, isOpen };
});
},
buttonRef: null,
setButtonRef(ref) {
console.log("Setting button ref");
set({ buttonRef: ref });
},
};
});

function DevToolsDialog({ isOpen, setIsOpen, children }) {
if (!isOpen) return null;

const Window = loadable(() =>
import("./_inner").then((mod) => mod.PageAdminInner)
);

return h(Window, { isOpen, setIsOpen }, children);
}

export function DevToolsConsole({ className, children }) {
const [isOpen, setIsOpen] = useIsOpen();
const buttonRef = useStore((state) => state.buttonRef);
useDevToolsKeyBinding();

const isSystemEnabled = useStore((state) => state.isSystemEnabled);

if (!isSystemEnabled) return null;

return h("div", { className }, [
h(DevToolsDialog, { isOpen, setIsOpen }, children),
h.if(buttonRef == null)(DevToolsButtonSlot, { setRef: false }),
]);
}

export function DevToolsButtonSlot({ setRef = true, className }) {
const onClick = useStore((state) => state.toggle);
const _setRef = useStore((state) => state.setButtonRef);
const isShown = useStore((state) => state.isSystemEnabled);
const ref = (el) => {
if (setRef) _setRef(el);
};

if (!isShown) return null;

return h(
"div.devtools-button",
{ className },
h(Button, {
onClick,
ref,
minimal: true,
icon: "code",
})
);
}

function useDevToolsKeyBinding() {
// Show the page admin console only if the appropriate query parameter is set
// OR if the user presses shift+alt+I
const toggleButton = useStore((s) => s.toggleButton);

useEffect(() => {
function handleKeyDown(event: KeyboardEvent) {
if (event.key === "I" && event.shiftKey) {
toggleButton();
}
}
document.addEventListener("keydown", handleKeyDown);
return () => document.removeEventListener("keydown", handleKeyDown);
}, []);
}

function useIsOpen(): [boolean, (isOpen: boolean) => void] {
return useStore((state) => [state.isOpen, state.setIsOpen]);
}
10 changes: 10 additions & 0 deletions packages/ui-components/src/dev-panel/page-admin.module.sass
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.dialog-content
padding: 1em
overflow-y: scroll

.page-admin
max-height: 80vh

.page-admin-button
width: 16px
height: 16px
1 change: 1 addition & 0 deletions packages/ui-components/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ export * from "./patterns";
export * from "./collapse-card";
export * from "./modal-panel";
export * from "./page-layouts";
export * from "./dev-panel";
14 changes: 11 additions & 3 deletions packages/ui-components/src/util/json-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,25 @@ const monokai = {
};

export function JSONView(props) {
const { hideRoot, showRoot, className, ...rest } = props;

let _hideRoot = hideRoot;
if (showRoot === false) {
_hideRoot = true;
}

return h(
"div.json-view-container",
{
className: classNames(props.className, {
"root-hidden": props.hideRoot,
className: classNames(className, {
"root-hidden": _hideRoot,
}),
},
h(JSONTree, {
theme: monokai,
invertTheme: false,
...props,
hideRoot: _hideRoot,
...rest,
})
);
}

0 comments on commit 38671da

Please sign in to comment.