@cedgetec-utils/astro-components 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # Astro Starter Kit: Component Package
2
+
3
+ This is a template for an Astro component library. Use this template for writing components to use in multiple projects or publish to NPM.
4
+
5
+ ```sh
6
+ bun create astro@latest -- --template component
7
+ ```
8
+
9
+ [![Open in StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz.svg)](https://stackblitz.com/github/withastro/astro/tree/latest/examples/non-html-pages)
10
+ [![Open with CodeSandbox](https://assets.codesandbox.io/github/button-edit-lime.svg)](https://codesandbox.io/p/sandbox/github/withastro/astro/tree/latest/examples/non-html-pages)
11
+ [![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/withastro/astro?devcontainer_path=.devcontainer/component/devcontainer.json)
12
+
13
+ ## 🚀 Project Structure
14
+
15
+ Inside of your Astro project, you'll see the following folders and files:
16
+
17
+ ```text
18
+ /
19
+ ├── index.ts
20
+ ├── src
21
+ │ └── MyComponent.astro
22
+ ├── tsconfig.json
23
+ ├── package.json
24
+ ```
25
+
26
+ The `index.ts` file is the "entry point" for your package. Export your components in `index.ts` to make them importable from your package.
27
+
28
+ ## 🧞 Commands
29
+
30
+ All commands are run from the root of the project, from a terminal:
31
+
32
+ | Command | Action |
33
+ | :------------ | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
34
+ | `bun link` | Registers this package locally. Run `bun link my-component-library` in an Astro project to install your components |
35
+ | `bun publish` | [Publishes](https://docs.npmjs.com/creating-and-publishing-unscoped-public-packages#publishing-unscoped-public-packages) this package to NPM. Requires you to be [logged in](https://docs.npmjs.com/cli/v8/commands/bun-adduser) |
package/index.ts ADDED
@@ -0,0 +1,6 @@
1
+ // Do not write code directly here, instead use the `src` folder!
2
+ // Then, use this file to export everything you want your user to access.
3
+
4
+ export * as DirectusImage from "./src/DirectusImage.astro";
5
+ export * as Head from "./src/Head.astro";
6
+ export * as Matomo from "./src/Matomo.astro";
package/package.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "name": "@cedgetec-utils/astro-components",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "exports": {
6
+ ".": "./index.ts"
7
+ },
8
+ "files": [
9
+ "src",
10
+ "index.ts"
11
+ ],
12
+ "keywords": [
13
+ "astro-component"
14
+ ],
15
+ "scripts": {},
16
+ "devDependencies": {
17
+ "astro": "^5.15.3"
18
+ },
19
+ "peerDependencies": {
20
+ "astro": "^5.0.0"
21
+ }
22
+ }
@@ -0,0 +1,71 @@
1
+ ---
2
+ import { Image } from "astro:assets";
3
+ import type { HTMLAttributes } from "astro/types";
4
+
5
+ export type DirectusImageData = {
6
+ id: string;
7
+ type: string;
8
+ title: string;
9
+ url: string;
10
+ modified_on: string;
11
+ width?: number | null | undefined;
12
+ height?: number | null | undefined;
13
+ };
14
+
15
+ type Props = {
16
+ src: DirectusImageData;
17
+ width?: number;
18
+ height?: number;
19
+ } & Omit<HTMLAttributes<"img">, "src">;
20
+ const { src, width, height, ...props } = Astro.props;
21
+
22
+ let calculatedWidth = src.width;
23
+ let calculatedHeight = src.height;
24
+
25
+ if (src.width && src.height) {
26
+ // Check if width and height were both specified
27
+ if (width !== undefined && height !== undefined) {
28
+ const ratio = width / height;
29
+ const inferredRatio = src.width / src.height;
30
+
31
+ if (inferredRatio > ratio) {
32
+ // Image is wider than container - scale by height
33
+ calculatedHeight = height;
34
+ calculatedWidth = height * inferredRatio;
35
+ } else {
36
+ // Image is taller than container - scale by width
37
+ calculatedWidth = width;
38
+ calculatedHeight = width * inferredRatio;
39
+ }
40
+ } else {
41
+ // Check if only width was specified
42
+ if (width !== undefined) {
43
+ calculatedWidth = width;
44
+ calculatedHeight = (src.height / src.width) * width;
45
+ }
46
+
47
+ // Check if only height was specified
48
+ if (height !== undefined) {
49
+ calculatedHeight = height;
50
+ calculatedWidth = (src.width / src.height) * height;
51
+ }
52
+ }
53
+ }
54
+ ---
55
+
56
+ {
57
+ src.type === "image/svg+xml" ? (
58
+ <img {...props} src={src.url} alt={src.title} height={height} />
59
+ ) : (
60
+ calculatedWidth &&
61
+ calculatedHeight && (
62
+ <Image
63
+ {...props}
64
+ src={`${src.url}?v=${src.modified_on}`}
65
+ alt={src.title}
66
+ width={calculatedWidth}
67
+ height={calculatedHeight}
68
+ />
69
+ )
70
+ )
71
+ }
package/src/Head.astro ADDED
@@ -0,0 +1,77 @@
1
+ ---
2
+ import { getImage } from "astro:assets";
3
+
4
+ type Props = {
5
+ title: string;
6
+ siteName: string;
7
+ type: "website" | string;
8
+ description: string | null | undefined;
9
+ keywords: string[] | null | undefined;
10
+ icons: {
11
+ lightUrl: string;
12
+ darkUrl: string;
13
+ };
14
+ imageUrl: string | null | undefined;
15
+ };
16
+ const props = Astro.props;
17
+
18
+ const slug = Astro.url.pathname.replace(/^\/+|\/+$/g, "");
19
+
20
+ const title =
21
+ slug === "" ? props.siteName : `${props.title} - ${props.siteName}`;
22
+
23
+ const ogTitle = slug === "" ? props.siteName : `${props.title}`;
24
+
25
+ const iconLight = await getImage({
26
+ src: props.icons.lightUrl,
27
+ width: 48,
28
+ height: 48,
29
+ fit: "contain",
30
+ });
31
+
32
+ const iconDark = await getImage({
33
+ src: props.icons.darkUrl,
34
+ width: 48,
35
+ height: 48,
36
+ fit: "contain",
37
+ });
38
+
39
+ const openGraphImage = props.imageUrl
40
+ ? await getImage({
41
+ src: props.imageUrl,
42
+ width: 1000,
43
+ height: 500,
44
+ })
45
+ : null;
46
+ ---
47
+
48
+ <head>
49
+ <meta charset="UTF-8" />
50
+ <meta name="viewport" content="width=device-width" />
51
+ <!-- Icons -->
52
+ <link rel="icon" href={iconDark.src} />
53
+ <link rel="icon" href={iconDark.src} media="(prefers-color-scheme:light)" />
54
+ <link rel="icon" href={iconLight.src} media="(prefers-color-scheme:dark)" />
55
+ <link rel="icon" href={iconDark.src} />
56
+ <meta name="generator" content={Astro.generator} />
57
+ <meta name="view-transition" content="same-origin" />
58
+ <!-- Title etc. -->
59
+ <title>{title}</title>
60
+ {props.description && <meta name="description" content={props.description} />}
61
+ {
62
+ props.keywords && (
63
+ <meta name="keywords" content={props.keywords.join(",")} />
64
+ )
65
+ }
66
+ <!-- Open Graph -->
67
+ <meta property="og:title" content={ogTitle} />
68
+ {
69
+ props.description && (
70
+ <meta property="og:description" content={props.description} />
71
+ )
72
+ }
73
+ {props.siteName && <meta property="og:site_name" content={props.siteName} />}
74
+ {props.type && <meta property="og:type" content={props.type} />}
75
+ {openGraphImage && <meta property="og:image" content={openGraphImage.src} />}
76
+ <slot />
77
+ </head>
@@ -0,0 +1,25 @@
1
+ ---
2
+ type Props = {
3
+ url: string;
4
+ siteId: number;
5
+ };
6
+ const { url, siteId } = Astro.props;
7
+ ---
8
+
9
+ <script define:vars={{ url, siteId }}>
10
+ const _paq = (window._paq = window._paq || []);
11
+ _paq.push(["trackPageView"]);
12
+ _paq.push(["enableLinkTracking"]);
13
+ (function () {
14
+ const u = `${url}/`;
15
+ _paq.push(["setTrackerUrl", u + "matomo.php"]);
16
+ _paq.push(["setSiteId", siteId]);
17
+ const d = document,
18
+ g = d.createElement("script"),
19
+ s = d.getElementsByTagName("script")[0];
20
+ g.type = "text/javascript";
21
+ g.async = true;
22
+ g.src = u + "matomo.js";
23
+ s.parentNode?.insertBefore(g, s);
24
+ })();
25
+ </script>