commit e10eb13e6674495402688e4e95b8ad5a4b62ddc1 Author: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Date: Mon Dec 16 19:52:24 2024 +0000 Sync from a44cfb874a6f066214e851c98a410d89c6866992 diff --git a/.codesandbox/Dockerfile b/.codesandbox/Dockerfile new file mode 100644 index 0000000000..c3b5c81a12 --- /dev/null +++ b/.codesandbox/Dockerfile @@ -0,0 +1 @@ +FROM node:18-bullseye diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000..16d54bb13c --- /dev/null +++ b/.gitignore @@ -0,0 +1,24 @@ +# build output +dist/ +# generated types +.astro/ + +# dependencies +node_modules/ + +# logs +npm-debug.log* +yarn-debug.log* +yarn-error.log* +pnpm-debug.log* + + +# environment variables +.env +.env.production + +# macOS-specific files +.DS_Store + +# jetbrains setting folder +.idea/ diff --git a/README.md b/README.md new file mode 100644 index 0000000000..163c9129a1 --- /dev/null +++ b/README.md @@ -0,0 +1,11 @@ +# Astro Example: Nanostores + +```sh +npm create astro@latest -- --template with-nanostores +``` + +[![Open in StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz.svg)](https://stackblitz.com/github/withastro/astro/tree/latest/examples/with-nanostores) +[![Open with CodeSandbox](https://assets.codesandbox.io/github/button-edit-lime.svg)](https://codesandbox.io/p/sandbox/github/withastro/astro/tree/latest/examples/with-nanostores) +[![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/withastro/astro?devcontainer_path=.devcontainer/with-nanostores/devcontainer.json) + +This example showcases using [`nanostores`](https://github.com/nanostores/nanostores) to provide shared state between components of any framework. [**Read our documentation on sharing state**](https://docs.astro.build/en/core-concepts/sharing-state/) for a complete breakdown of this project, along with guides to use React, Vue, Svelte, or Solid! diff --git a/astro.config.mjs b/astro.config.mjs new file mode 100644 index 0000000000..9f7dbd219c --- /dev/null +++ b/astro.config.mjs @@ -0,0 +1,9 @@ +// @ts-check +import { defineConfig } from 'astro/config'; +import preact from '@astrojs/preact'; + +// https://astro.build/config +export default defineConfig({ + // Enable many frameworks to support all different kinds of components. + integrations: [preact()], +}); diff --git a/package.json b/package.json new file mode 100644 index 0000000000..7f636e4211 --- /dev/null +++ b/package.json @@ -0,0 +1,19 @@ +{ + "name": "@example/with-nanostores", + "type": "module", + "version": "0.0.1", + "private": true, + "scripts": { + "dev": "astro dev", + "build": "astro build", + "preview": "astro preview", + "astro": "astro" + }, + "dependencies": { + "@astrojs/preact": "^4.0.0", + "@nanostores/preact": "^0.5.2", + "astro": "^5.0.8", + "nanostores": "^0.11.3", + "preact": "^10.24.3" + } +} diff --git a/public/favicon.svg b/public/favicon.svg new file mode 100644 index 0000000000..f157bd1c5e --- /dev/null +++ b/public/favicon.svg @@ -0,0 +1,9 @@ + + + + diff --git a/public/images/astronaut-figurine.png b/public/images/astronaut-figurine.png new file mode 100644 index 0000000000..aac9b445ed Binary files /dev/null and b/public/images/astronaut-figurine.png differ diff --git a/src/cartStore.ts b/src/cartStore.ts new file mode 100644 index 0000000000..a57a6ce875 --- /dev/null +++ b/src/cartStore.ts @@ -0,0 +1,31 @@ +import { atom, map } from 'nanostores'; + +export const isCartOpen = atom(false); + +export type CartItem = { + id: string; + name: string; + imageSrc: string; + quantity: number; +}; + +export type CartItemDisplayInfo = Pick; + +export const cartItems = map>({}); + +export function addCartItem({ id, name, imageSrc }: CartItemDisplayInfo) { + const existingEntry = cartItems.get()[id]; + if (existingEntry) { + cartItems.setKey(id, { + ...existingEntry, + quantity: existingEntry.quantity + 1, + }); + } else { + cartItems.setKey(id, { + id, + name, + imageSrc, + quantity: 1, + }); + } +} diff --git a/src/components/AddToCartForm.tsx b/src/components/AddToCartForm.tsx new file mode 100644 index 0000000000..7498443f67 --- /dev/null +++ b/src/components/AddToCartForm.tsx @@ -0,0 +1,18 @@ +import { isCartOpen, addCartItem } from '../cartStore'; +import type { CartItemDisplayInfo } from '../cartStore'; +import type { ComponentChildren } from 'preact'; + +type Props = { + item: CartItemDisplayInfo; + children: ComponentChildren; +}; + +export default function AddToCartForm({ item, children }: Props) { + function addToCart(e: SubmitEvent) { + e.preventDefault(); + isCartOpen.set(true); + addCartItem(item); + } + + return
{children}
; +} diff --git a/src/components/CartFlyout.module.css b/src/components/CartFlyout.module.css new file mode 100644 index 0000000000..cee43dd4ca --- /dev/null +++ b/src/components/CartFlyout.module.css @@ -0,0 +1,29 @@ +.container { + position: fixed; + right: 0; + top: var(--nav-height); + height: 100vh; + background: var(--color-bg-2); + padding-inline: 2rem; + min-width: min(90vw, 300px); + border-left: 3px solid var(--color-bg-3); +} + +.list { + list-style: none; + padding: 0; +} + +.listItem { + display: flex; + gap: 1rem; + align-items: center; +} + +.listItem * { + margin-block: 0.3rem; +} + +.listItemImg { + width: 4rem; +} diff --git a/src/components/CartFlyout.tsx b/src/components/CartFlyout.tsx new file mode 100644 index 0000000000..98fd8cbfb3 --- /dev/null +++ b/src/components/CartFlyout.tsx @@ -0,0 +1,28 @@ +import { useStore } from '@nanostores/preact'; +import { cartItems, isCartOpen } from '../cartStore'; +import styles from './CartFlyout.module.css'; + +export default function CartFlyout() { + const $isCartOpen = useStore(isCartOpen); + const $cartItems = useStore(cartItems); + + return ( + + ); +} diff --git a/src/components/CartFlyoutToggle.tsx b/src/components/CartFlyoutToggle.tsx new file mode 100644 index 0000000000..14ce1c70d8 --- /dev/null +++ b/src/components/CartFlyoutToggle.tsx @@ -0,0 +1,7 @@ +import { useStore } from '@nanostores/preact'; +import { isCartOpen } from '../cartStore'; + +export default function CartFlyoutToggle() { + const $isCartOpen = useStore(isCartOpen); + return ; +} diff --git a/src/components/FigurineDescription.astro b/src/components/FigurineDescription.astro new file mode 100644 index 0000000000..1294b15100 --- /dev/null +++ b/src/components/FigurineDescription.astro @@ -0,0 +1,44 @@ +

Astronaut Figurine

+

Limited Edition

+

+ The limited edition Astronaut Figurine is the perfect gift for any Astro contributor. This + fully-poseable action figurine comes equipped with: +

+ +

+ * Dust not actually from the lunar surface +

+ + diff --git a/src/layouts/Layout.astro b/src/layouts/Layout.astro new file mode 100644 index 0000000000..aa8c34773f --- /dev/null +++ b/src/layouts/Layout.astro @@ -0,0 +1,113 @@ +--- +import CartFlyout from '../components/CartFlyout'; +import CartFlyoutToggle from '../components/CartFlyoutToggle'; +import { withBase } from '../utils'; + +interface Props { + title: string; +} + +const { title } = Astro.props; +--- + + + + + + + + + {title} + + +
+ +
+ + + + + + + + diff --git a/src/pages/index.astro b/src/pages/index.astro new file mode 100644 index 0000000000..c906095950 --- /dev/null +++ b/src/pages/index.astro @@ -0,0 +1,50 @@ +--- +import type { CartItemDisplayInfo } from '../cartStore'; +import Layout from '../layouts/Layout.astro'; +import AddToCartForm from '../components/AddToCartForm'; +import FigurineDescription from '../components/FigurineDescription.astro'; +import { withBase } from '../utils'; + +const item: CartItemDisplayInfo = { + id: 'astronaut-figurine', + name: 'Astronaut Figurine', + imageSrc: withBase('/images/astronaut-figurine.png'), +}; +--- + + +
+
+
+ + + + +
+ {item.name} +
+
+
+ + diff --git a/src/utils.ts b/src/utils.ts new file mode 100644 index 0000000000..cbe38f5f3e --- /dev/null +++ b/src/utils.ts @@ -0,0 +1,4 @@ +const base = import.meta.env.BASE_URL.replace(/\/$/, ''); + +/** Prefix a URL path with the site’s base path if set. */ +export const withBase = (path: string) => base + path; diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000000..c8983c2ef0 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,10 @@ +{ + "extends": "astro/tsconfigs/strict", + "include": [".astro/types.d.ts", "**/*"], + "exclude": ["dist"], + "compilerOptions": { + // Preact specific settings + "jsx": "react-jsx", + "jsxImportSource": "preact" + } +}