Skip to content

Commit

Permalink
refactor tooltip
Browse files Browse the repository at this point in the history
  • Loading branch information
nattthebear committed Jul 14, 2023
1 parent ea19b9f commit f70fe99
Show file tree
Hide file tree
Showing 12 changed files with 84 additions and 85 deletions.
2 changes: 2 additions & 0 deletions docs/index.1445ecdb.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 0 additions & 10 deletions docs/index.203c95b7.js

This file was deleted.

2 changes: 0 additions & 2 deletions docs/index.928c49fb.css

This file was deleted.

10 changes: 10 additions & 0 deletions docs/index.9c03c1ec.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"><meta name="theme-color" content="#000000"><link rel="icon" href="data:,"><title>Final Fantasy 12 Character Planner</title><link rel="stylesheet" href="index.928c49fb.css"><script defer type="module" src="index.203c95b7.js"></script></head><body></body></html>
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"><meta name="theme-color" content="#000000"><link rel="icon" href="data:,"><title>Final Fantasy 12 Character Planner</title><link rel="stylesheet" href="index.1445ecdb.css"><script defer type="module" src="index.9c03c1ec.js"></script></head><body></body></html>
34 changes: 0 additions & 34 deletions src/MouseOver.ts

This file was deleted.

27 changes: 0 additions & 27 deletions src/component/App.tsx

This file was deleted.

25 changes: 20 additions & 5 deletions src/component/CharacterPlanner.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,36 @@
import { h, TPC } from "vdomk";
import CharacterPanel from "./CharacterPanel";
import LicenseBoard from "./LicenseBoard";
import "./CharacterPlanner.css";
import QeBoard from "./QeBoard";
import Dps from "./Dps";
import { useStore } from "../store/Store";
import PartyModel from "../model/PartyModel";

import "./CharacterPlanner.css";

const CharacterPlanner: TPC<{}> = (_, instance) => {
const getState = useStore(instance);
let prevParty: PartyModel | undefined;

return () => {
const store = getState();
const { party, qeActive, dpsActive } = getState();

if (party !== prevParty) {
requestIdleCallback(() => {
const urlBase = window.location.href.split("?")[0];
const search = party.encode();
const urlSuffix = search === "AA.AA.AA.AA.AA.AA" ? "" : "?" + search;
window.history.replaceState(null, "", urlBase + urlSuffix);
});
prevParty = party;
}

return <div class="character-planner">
<CharacterPanel />
{store.qeActive
{qeActive
? <QeBoard />
: store.dpsActive
? <Dps party={store.party} />
: dpsActive
? <Dps party={party} />
: <LicenseBoard />
}
</div>;
Expand Down
4 changes: 4 additions & 0 deletions src/MouseOver.css → src/component/MouseOver.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@
border-radius: 6px;
background-color: rgba(0, 0, 0, 0.7);
pointer-events: none;

&.hidden {
display: none;
}
}
35 changes: 35 additions & 0 deletions src/component/MouseOver.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { computePosition, shift, flip } from "@floating-ui/dom";
import { TPC, effect, h, scheduleUpdate } from "vdomk";
import "./MouseOver.css";

const Tooltip: TPC<{}> = (_, instance) => {
let floatingElement: HTMLDivElement | null = null;
const ref = (value: HTMLDivElement | null) => floatingElement = value;

let labelText: string | null = null;
let referenceElement: Element | null = null;

document.addEventListener("mouseover", event => {
referenceElement = event.target as Element | null;
while (referenceElement && (labelText = referenceElement.getAttribute("aria-label")) == null) {
referenceElement = referenceElement.parentElement;
}
scheduleUpdate(instance);
}, { passive: true });

async function updateStyles() {
if (referenceElement && floatingElement) {
const styles = await computePosition(referenceElement, floatingElement, { middleware: [flip(), shift()] });
floatingElement.style.transform = `translate(${Math.round(styles.x)}px,${Math.round(styles.y)}px)`;
}
}

document.addEventListener("scroll", updateStyles, { passive: true, capture: true });

return () => {
effect(instance, updateStyles);
return <div ref={ref} class={labelText ? "tooltip" : "tooltip hidden"}>{labelText}</div>;
};
};

export default Tooltip;
3 changes: 2 additions & 1 deletion src/component/App.css → src/component/shared.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* Global and shared styles */

button {
cursor: pointer;
&:disabled {
Expand All @@ -13,7 +15,6 @@ button {
font-family: "HelveticaNeue-Light", "Helvetica Neue Light", "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif;
}

/* Some shared styles */
.button {
margin: 2px;
border: 2px solid #000;
Expand Down
15 changes: 10 additions & 5 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,17 @@ if (!window.requestIdleCallback) {
};
}

import { h, createRoot } from "vdomk";
import App from "./component/App";
import "./MouseOver";
import "modern-normalize/modern-normalize.css";
import "./component/shared.css";

import { h, Fragment, createRoot } from "vdomk";
import CharacterPlanner from "./component/CharacterPlanner";
import MouseOver from "./component/MouseOver";

createRoot(
document.body,
<App />,
document.body.lastChild // tooltip must come after the app root
<>
<CharacterPlanner />
<MouseOver />
</>
);

0 comments on commit f70fe99

Please sign in to comment.