-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #77 from bholmesdev/feat/root-element
Feat/root element
- Loading branch information
Showing
43 changed files
with
15,472 additions
and
3,627 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
--- | ||
"simple-stack-query": minor | ||
--- | ||
|
||
Revamps APIs to fix bugs and unlock a new suite of features. | ||
|
||
```astro | ||
<RootElement> | ||
<button data-target="btn">Click me</button> | ||
</RootElement> | ||
<script> | ||
RootElement.ready(($) => { | ||
$('btn').addEventListener('click', () => { | ||
console.log("It's like JQuery but not!"); | ||
}); | ||
}); | ||
</script> | ||
``` | ||
|
||
- Support multiple instances of the same component. Before, only the first instance would become interactive. | ||
- Enable data passing from the server to your client script using the `data` property. | ||
- Add an `effect()` utility to interact with the [Signal polyfill](https://github.com/proposal-signals/signal-polyfill?tab=readme-ov-file#creating-a-simple-effect) for state management. | ||
|
||
[Visit revamped documentation page](https://simple-stack.dev/query) to learn how to use the new features. | ||
|
||
## Migration for v0.1 | ||
|
||
If you were an early adopter of v0.1, thank you! You'll a few small updates to use the new APIs: | ||
|
||
- Wrap any HTML you want to target with the global `RootElement` component. | ||
- Remove the `$` from your `data-target` selector (`data-target={$('btn')}` -> `data-target="btn"`). Scoping is now handled automatically. | ||
- Change `$.ready()` to `RootElement.ready()`, and retrieve the `$` selector from the first function argument. The `$` selector is no longer a global. | ||
|
||
```diff | ||
+ <RootElement> | ||
- <button data=target={$('btn')}> | ||
+ <button data-target="btn"> | ||
Click me | ||
</button> | ||
+ </RootElement> | ||
|
||
<script> | ||
- $.ready(() => { | ||
+ RootElement.ready(($) => { | ||
$('btn').addEventListener('click', () => { | ||
console.log("It's like JQuery but not!"); | ||
}); | ||
}); | ||
</script> | ||
``` | ||
|
||
Since the syntax for `data-target` is now simpler, we have also **removed the VS Code snippets prompt.** We recommend deleting the snippets file created by v0.1: `.vscode/simple-query.code-snippets`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
node_modules/ | ||
/test-results/ | ||
/playwright-report/ | ||
/blob-report/ | ||
/playwright/.cache/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,23 @@ | ||
declare function $<T extends Element = HTMLElement>(selector: string): T; | ||
|
||
declare namespace $ { | ||
function all<T extends Element = HTMLElement>(selector: string): Array<T>; | ||
function optional<T extends Element = HTMLElement>( | ||
selector: string, | ||
): T | undefined; | ||
function ready(callback: () => void): void; | ||
declare namespace RootElement { | ||
function ready<T extends Record<string, any>>( | ||
callback: ( | ||
$: { | ||
<T extends Element = HTMLElement>(selector: string): T; | ||
self: HTMLElement; | ||
all<T extends Element = HTMLElement>(selector: string): Array<T>; | ||
optional<T extends Element = HTMLElement>( | ||
selector: string, | ||
): T | undefined; | ||
}, | ||
context: { | ||
effect: (callback: () => void | Promise<void>) => void; | ||
data: T; | ||
abortSignal: AbortSignal; | ||
}, | ||
) => void, | ||
); | ||
} | ||
|
||
declare function RootElement<T extends Record<string, any>>( | ||
props: import("astro/types").HTMLAttributes<"div"> & { data?: T }, | ||
): any | Promise<any>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { expect, test } from "@playwright/test"; | ||
import { type PreviewServer, preview } from "astro"; | ||
import { generatePort, getPath } from "./utils"; | ||
|
||
const fixtureRoot = new URL("../fixtures/basic", import.meta.url).pathname; | ||
let previewServer: PreviewServer; | ||
|
||
test.beforeAll(async () => { | ||
previewServer = await preview({ | ||
root: fixtureRoot, | ||
server: { port: await generatePort() }, | ||
}); | ||
}); | ||
|
||
test.afterAll(async () => { | ||
await previewServer.stop(); | ||
}); | ||
|
||
test("loads client JS for heading", async ({ page }) => { | ||
await page.goto(getPath("", previewServer)); | ||
|
||
const h1 = page.getByTestId("heading"); | ||
await expect(h1).toContainText("Heading JS loaded"); | ||
}); | ||
|
||
test("reacts to button click", async ({ page }) => { | ||
await page.goto(getPath("button", previewServer)); | ||
|
||
const btn = page.getByRole("button"); | ||
|
||
await expect(btn).toHaveAttribute("data-ready"); | ||
await btn.click(); | ||
await expect(btn).toContainText("1"); | ||
}); | ||
|
||
test("reacts to button effect", async ({ page }) => { | ||
await page.goto(getPath("effect", previewServer)); | ||
|
||
const btn = page.getByRole("button"); | ||
|
||
await expect(btn).toHaveAttribute("data-ready"); | ||
await btn.click(); | ||
await expect(btn).toContainText("1"); | ||
const p = page.getByRole("paragraph"); | ||
await expect(p).toContainText("1"); | ||
}); | ||
|
||
test("respects server data", async ({ page }) => { | ||
await page.goto(getPath("server-data", previewServer)); | ||
|
||
const h1 = page.getByTestId("heading"); | ||
await expect(h1).toContainText("Server data"); | ||
}); | ||
|
||
test("reacts to multiple instances of button counter", async ({ page }) => { | ||
await page.goto(getPath("multi-counter", previewServer)); | ||
|
||
for (const testId of ["counter-1", "counter-2"]) { | ||
const counter = page.getByTestId(testId); | ||
const btn = counter.getByRole("button"); | ||
|
||
await expect(btn).toHaveAttribute("data-ready"); | ||
await expect(btn).toContainText("0"); | ||
await btn.click(); | ||
await expect(btn).toContainText("1"); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import net from "node:net"; | ||
import { type PreviewServer } from "astro"; | ||
|
||
export function isPortAvailable(port) { | ||
return new Promise((resolve) => { | ||
const server = net.createServer(); | ||
|
||
server.once("error", (err) => { | ||
if ("code" in err && err.code === "EADDRINUSE") { | ||
resolve(false); | ||
} | ||
}); | ||
|
||
server.once("listening", () => { | ||
server.close(); | ||
resolve(true); | ||
}); | ||
|
||
server.listen(port); | ||
}); | ||
} | ||
|
||
export async function generatePort() { | ||
const port = Math.floor(Math.random() * 1000) + 9000; | ||
if (await isPortAvailable(port)) return port; | ||
|
||
return generatePort(); | ||
} | ||
|
||
export function getPath(path: string, previewServer: PreviewServer) { | ||
return new URL(path, `http://localhost:${previewServer.port}/`).href; | ||
} |
Oops, something went wrong.