@datocms/astro 0.1.6 → 0.1.7

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/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@datocms/astro",
3
3
  "description": "A set of components and utilities to work faster with DatoCMS in Astro projects.",
4
4
  "type": "module",
5
- "version": "0.1.6",
5
+ "version": "0.1.7",
6
6
  "sideEffects": false,
7
7
  "repository": {
8
8
  "type": "git",
@@ -1,45 +1,9 @@
1
1
  ---
2
- import type { JSX } from 'astro/jsx-runtime';
3
- import type { ResponsiveImageType } from './types';
4
- import { buildSrcSet } from './buildSrcSet';
5
2
 
6
- type StyleAttribute = JSX.HTMLAttributes['style'];
7
- type StyleAttributeAsObject = Exclude<StyleAttribute, string>;
3
+ import { buildSrcSet } from './buildSrcSet';
4
+ import type { ImageProps } from './types';
8
5
 
9
- interface Props {
10
- /** The actual response you get from a DatoCMS `responsiveImage` GraphQL query */
11
- data: ResponsiveImageType;
12
- /** Additional CSS class for the root <picture> tag */
13
- pictureClass?: string;
14
- /** Additional CSS rules to add to the root <picture> tag */
15
- pictureStyle?: StyleAttribute;
16
- /** Additional CSS class for the <img> tag */
17
- imgClass?: string;
18
- /** Additional CSS rules to add to the <img> tag */
19
- imgStyle?: StyleAttributeAsObject;
20
- /**
21
- * When true, the image will be considered high priority. Lazy loading is automatically disabled, and fetchpriority="high" is added to the image.
22
- * You should use the priority property on any image detected as the Largest Contentful Paint (LCP) element. It may be appropriate to have multiple priority images, as different images may be the LCP element for different viewport sizes.
23
- * Should only be used when the image is visible above the fold.
24
- **/
25
- priority?: boolean;
26
- /** Whether the component should use a blurred image placeholder */
27
- usePlaceholder?: boolean;
28
- /**
29
- * The HTML5 `sizes` attribute for the image
30
- *
31
- * Learn more about srcset and sizes:
32
- * -> https://web.dev/learn/design/responsive-images/#sizes
33
- * -> https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#attr-sizes
34
- **/
35
- sizes?: HTMLImageElement["sizes"];
36
- /**
37
- * If `data` does not contain `srcSet`, the candidates for the `srcset` of the image will be auto-generated based on these width multipliers
38
- *
39
- * Default candidate multipliers are [0.25, 0.5, 0.75, 1, 1.5, 2, 3, 4]
40
- **/
41
- srcSetCandidates?: number[];
42
- }
6
+ interface Props extends ImageProps {};
43
7
 
44
8
  const {
45
9
  data,
@@ -50,10 +14,10 @@ const {
50
14
  priority,
51
15
  usePlaceholder = true,
52
16
  sizes,
53
- srcSetCandidates,
17
+ srcSetCandidates = [0.25, 0.5, 0.75, 1, 1.5, 2, 3, 4],
54
18
  } = Astro.props;
55
19
 
56
- const placeholderStyle: StyleAttributeAsObject | undefined =
20
+ const placeholderStyle =
57
21
  usePlaceholder && data.base64
58
22
  ? {
59
23
  backgroundImage: `url("${data.base64}")`,
@@ -10,7 +10,7 @@ export const buildSrcSet = (
10
10
  return undefined;
11
11
  }
12
12
 
13
- return candidateMultipliers
13
+ return (candidateMultipliers || [])
14
14
  .map((multiplier) => {
15
15
  const url = new URL(src, bogusBaseUrl);
16
16
 
@@ -1,3 +1,8 @@
1
+ import type { JSX } from 'astro/jsx-runtime';
2
+
3
+ type StyleAttribute = JSX.HTMLAttributes['style'];
4
+ type StyleAttributeAsObject = Exclude<StyleAttribute, string>;
5
+
1
6
  type Maybe<T> = T | null;
2
7
 
3
8
  export type ResponsiveImageType = {
@@ -24,3 +29,38 @@ export type ResponsiveImageType = {
24
29
  /** Title attribute (`title`) for the image */
25
30
  title?: Maybe<string>;
26
31
  };
32
+
33
+ export type ImageProps = {
34
+ /** The actual response you get from a DatoCMS `responsiveImage` GraphQL query */
35
+ data: ResponsiveImageType;
36
+ /** Additional CSS class for the root <picture> tag */
37
+ pictureClass?: string;
38
+ /** Additional CSS rules to add to the root <picture> tag */
39
+ pictureStyle?: StyleAttribute;
40
+ /** Additional CSS class for the <img> tag */
41
+ imgClass?: string;
42
+ /** Additional CSS rules to add to the <img> tag */
43
+ imgStyle?: StyleAttributeAsObject;
44
+ /**
45
+ * When true, the image will be considered high priority. Lazy loading is automatically disabled, and fetchpriority="high" is added to the image.
46
+ * You should use the priority property on any image detected as the Largest Contentful Paint (LCP) element. It may be appropriate to have multiple priority images, as different images may be the LCP element for different viewport sizes.
47
+ * Should only be used when the image is visible above the fold.
48
+ **/
49
+ priority?: boolean;
50
+ /** Whether the component should use a blurred image placeholder */
51
+ usePlaceholder?: boolean;
52
+ /**
53
+ * The HTML5 `sizes` attribute for the image
54
+ *
55
+ * Learn more about srcset and sizes:
56
+ * -> https://web.dev/learn/design/responsive-images/#sizes
57
+ * -> https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img#attr-sizes
58
+ **/
59
+ sizes?: HTMLImageElement["sizes"];
60
+ /**
61
+ * If `data` does not contain `srcSet`, the candidates for the `srcset` of the image will be auto-generated based on these width multipliers
62
+ *
63
+ * Default candidate multipliers are [0.25, 0.5, 0.75, 1, 1.5, 2, 3, 4]
64
+ **/
65
+ srcSetCandidates?: number[];
66
+ }
@@ -0,0 +1,30 @@
1
+ ---
2
+ import type { VideoPlayerProps } from "./types";
3
+ import { useVideoPlayer } from "../useVideoPlayer";
4
+ import { toNativeProps } from "./utils";
5
+
6
+ interface Props extends VideoPlayerProps {}
7
+
8
+ const {
9
+ data,
10
+ disableCookies = true,
11
+ preload = "metadata",
12
+ ...otherProps
13
+ } = Astro.props;
14
+
15
+ const computedProps = {
16
+ ...useVideoPlayer({ data }),
17
+ disableCookies,
18
+ preload,
19
+ };
20
+ ---
21
+
22
+ <mux-player
23
+ stream-type="on-demand"
24
+ {...toNativeProps(computedProps)}
25
+ {...toNativeProps(otherProps)}
26
+ ></mux-player>
27
+
28
+ <script>
29
+ import "@mux/mux-player";
30
+ </script>
@@ -1,2 +1,2 @@
1
- import Component from "./index.astro";
1
+ import Component from "./VideoPlayer.astro";
2
2
  export default Component;
@@ -1,20 +1,72 @@
1
+ import type { MuxMediaProps } from "@mux/playback-core";
2
+ import type { JSX } from 'astro/jsx-runtime';
3
+
4
+ type StyleAttribute = JSX.HTMLAttributes["style"];
5
+
1
6
  type Maybe<T> = T | null;
2
- type Possibly<T> = Maybe<T> | undefined;
3
7
 
4
8
  export type Video = {
5
9
  /** Title attribute (`title`) for the video */
6
- title?: Possibly<string>;
10
+ title?: Maybe<string>;
7
11
  /** The height of the video */
8
- height?: Possibly<number>;
12
+ height?: Maybe<number>;
9
13
  /** The width of the video */
10
- width?: Possibly<number>;
14
+ width?: Maybe<number>;
11
15
  /** The MUX playback ID */
12
- muxPlaybackId?: Possibly<string>;
16
+ muxPlaybackId?: Maybe<string>;
13
17
  /** The MUX playback ID */
14
- playbackId?: Possibly<string>;
18
+ playbackId?: Maybe<string>;
15
19
  /** A data: URI containing a blurhash for the video */
16
- blurUpThumb?: Possibly<string>;
20
+ blurUpThumb?: Maybe<string>;
17
21
  /** Other data can be passed, but they have no effect on rendering the player */
18
22
  // biome-ignore lint/suspicious/noExplicitAny: we intentionally want to allow to add any other value to this video object
19
23
  [k: string]: any;
24
+ };
25
+
26
+ // Extracted from mux-player-react
27
+
28
+ type VideoApiAttributes = {
29
+ currentTime: number;
30
+ volume: number;
31
+ paused: boolean;
32
+ src: string | null;
33
+ poster: string;
34
+ playbackRate: number;
35
+ playsInline: boolean;
36
+ preload: string;
37
+ crossOrigin: string;
38
+ autoPlay: boolean | string;
39
+ loop: boolean;
40
+ muted: boolean;
41
+ class?: string;
42
+ style?: StyleAttribute;
43
+ };
44
+
45
+ // Extracted from mux-player-react
46
+
47
+ export type MuxPlayerProps = Partial<MuxMediaProps> & Partial<VideoApiAttributes> & {
48
+ hotkeys?: string;
49
+ nohotkeys?: boolean;
50
+ defaultHiddenCaptions?: boolean;
51
+ forwardSeekOffset?: number;
52
+ backwardSeekOffset?: number;
53
+ metadataVideoId?: string;
54
+ metadataVideoTitle?: string;
55
+ metadataViewerUserId?: string;
56
+ primaryColor?: string;
57
+ secondaryColor?: string;
58
+ accentColor?: string;
59
+ placeholder?: string;
60
+ playbackRates?: number[];
61
+ defaultShowRemainingTime?: boolean;
62
+ defaultDuration?: number;
63
+ noVolumePref?: boolean;
64
+ thumbnailTime?: number;
65
+ title?: string;
66
+ theme?: string;
67
+ themeProps?: { [k: string]: any };
68
+ };
69
+
70
+ export type VideoPlayerProps = MuxPlayerProps & {
71
+ data: Video;
20
72
  };
@@ -0,0 +1,50 @@
1
+ // Adapted from mux-player-react
2
+
3
+ const ReactPropToAttrNameMap = {
4
+ crossOrigin: 'crossorigin',
5
+ viewBox: 'viewBox',
6
+ playsInline: 'playsinline',
7
+ autoPlay: 'autoplay',
8
+ playbackRate: 'playbackrate',
9
+ };
10
+
11
+ type KeyTypes = string | number | symbol;
12
+ type Maybe<T> = T | null | undefined;
13
+
14
+ const isNil = (x: unknown): x is null | undefined => x === undefined;
15
+
16
+ // Type Guard to determine if a given key is actually a key of some object of type T
17
+ export const isKeyOf = <T extends {} = any>(k: KeyTypes, o: Maybe<T>): k is keyof T => {
18
+ if (isNil(o)) return false;
19
+ return k in o;
20
+ };
21
+
22
+ const toKebabCase = (string: string) => string.replace(/[A-Z]/g, (match) => `-${match.toLowerCase()}`);
23
+
24
+ const toNativeAttrName = (propName: string, propValue: any): string | undefined => {
25
+ if (typeof propValue === 'boolean' && !propValue) return undefined;
26
+ if (isKeyOf(propName, ReactPropToAttrNameMap)) return ReactPropToAttrNameMap[propName];
27
+ if (propValue === undefined) return undefined;
28
+ if (/[A-Z]/.test(propName)) return toKebabCase(propName);
29
+ return propName;
30
+ };
31
+
32
+ const toNativeAttrValue = (propValue: any, propName: string) => {
33
+ if (typeof propValue === 'boolean') return '';
34
+ return propValue;
35
+ };
36
+
37
+ export const toNativeProps = (props = {}) => {
38
+ return Object.entries(props).reduce<{ [k: string]: string }>((transformedProps, [propName, propValue]) => {
39
+ const attrName = toNativeAttrName(propName, propValue);
40
+
41
+ // prop was stripped. Don't add.
42
+ if (!attrName) {
43
+ return transformedProps;
44
+ }
45
+
46
+ const attrValue = toNativeAttrValue(propValue, propName);
47
+ transformedProps[attrName] = attrValue;
48
+ return transformedProps;
49
+ }, {});
50
+ };
@@ -1,23 +1,27 @@
1
- import type { Video } from "../VideoPlayer/types";
1
+ import type { MuxPlayerProps, Video } from "../VideoPlayer/types";
2
2
 
3
3
  type Maybe<T> = T | null;
4
4
  type Possibly<T> = Maybe<T> | undefined;
5
5
 
6
- const computedTitle = (title: Maybe<string> | undefined) => {
7
- return title ? { title } : undefined;
6
+ function computedTitle(title: Possibly<string>): MuxPlayerProps {
7
+ return title ? { title } : {};
8
8
  };
9
9
 
10
- const computedPlaybackId = (
10
+ function computedPlaybackId(
11
11
  muxPlaybackId: Possibly<string>,
12
12
  playbackId: Possibly<string>,
13
- ) => {
14
- if (!(muxPlaybackId || playbackId)) return undefined;
13
+ ): MuxPlayerProps {
14
+ if (!(muxPlaybackId || playbackId)) {
15
+ return {};
16
+ }
15
17
 
16
18
  return { playbackId: `${muxPlaybackId || playbackId}` };
17
19
  };
18
20
 
19
- const computedStyle = (width: Possibly<number>, height: Possibly<number>) => {
20
- if (!(width && height)) return undefined;
21
+ function computedStyle(width: Possibly<number>, height: Possibly<number>): MuxPlayerProps {
22
+ if (!(width && height)) {
23
+ return {};
24
+ }
21
25
 
22
26
  return {
23
27
  style: {
@@ -26,17 +30,17 @@ const computedStyle = (width: Possibly<number>, height: Possibly<number>) => {
26
30
  };
27
31
  };
28
32
 
29
- const computedPlaceholder = (blurUpThumb: Possibly<string>) => {
30
- return blurUpThumb ? { placeholder: blurUpThumb } : undefined;
33
+ function computedPlaceholder(blurUpThumb: Possibly<string>): MuxPlayerProps {
34
+ return blurUpThumb ? { placeholder: blurUpThumb } : {};
31
35
  };
32
36
 
33
37
  type UseVideoPlayerArgs = {
34
38
  data?: Video;
35
39
  };
36
40
 
37
- export const useVideoPlayer = ({
41
+ export function useVideoPlayer({
38
42
  data,
39
- }: UseVideoPlayerArgs) => {
43
+ }: UseVideoPlayerArgs): MuxPlayerProps {
40
44
  const { title, width, height, playbackId, muxPlaybackId, blurUpThumb } =
41
45
  data || {};
42
46
 
@@ -1,37 +0,0 @@
1
- ---
2
- import type { MuxMediaProps } from "@mux/playback-core";
3
- import { toHTMLAttrs } from "./toHtmlArgs";
4
- import type { JSX } from 'astro/jsx-runtime';
5
- import type { Video } from "./types";
6
- import { useVideoPlayer } from "../useVideoPlayer";
7
-
8
- type StyleAttribute = JSX.HTMLAttributes["style"];
9
-
10
- interface Props extends Partial<MuxMediaProps> {
11
- data: Video;
12
- class?: string;
13
- style?: StyleAttribute;
14
- };
15
-
16
- const {
17
- data,
18
- disableCookies = true,
19
- preload = "metadata",
20
- ...otherProps
21
- } = Astro.props;
22
-
23
- const computedProps = {
24
- ...useVideoPlayer({ data }),
25
- disableCookies,
26
- preload,
27
- };
28
- ---
29
-
30
- <mux-player
31
- stream-type="on-demand"
32
- {...toHTMLAttrs(computedProps)}
33
- {...toHTMLAttrs(otherProps)}></mux-player>
34
-
35
- <script>
36
- import "@mux/mux-player";
37
- </script>
@@ -1,71 +0,0 @@
1
- // Extracted from https://github.com/muxinc/elements/blob/main/packages/mux-player-react/src/common/utils.ts
2
-
3
- type KeyTypes = string | number | symbol;
4
- type Maybe<T> = T | null;
5
-
6
- export const isNil = (x: unknown): x is null | undefined => x == undefined;
7
-
8
- // Type Guard to determine if a given key is actually a key of some object of type T
9
- export const isKeyOf = <T extends {} = any>(
10
- k: KeyTypes,
11
- o: Maybe<T> | undefined,
12
- ): k is keyof T => {
13
- if (isNil(o)) return false;
14
- return k in o;
15
- };
16
-
17
- const PropToAttrNameMap = {
18
- crossOrigin: 'crossorigin',
19
- viewBox: 'viewBox',
20
- playsInline: 'playsinline',
21
- autoPlay: 'autoplay',
22
- playbackRate: 'playbackrate',
23
- playbackRates: 'playbackrates',
24
- };
25
-
26
- const toKebabCase = (string: string) =>
27
- string.replace(/[A-Z]/g, (match) => `-${match.toLowerCase()}`);
28
-
29
- export const toNativeAttrName = (
30
- propName: string,
31
- propValue: any,
32
- ): string | undefined => {
33
- if (typeof propValue === 'boolean' && !propValue) return undefined;
34
- if (isKeyOf(propName, PropToAttrNameMap)) return PropToAttrNameMap[propName];
35
- if (typeof propValue === 'undefined') return undefined;
36
- if (/[A-Z]/.test(propName)) return toKebabCase(propName);
37
-
38
- return propName;
39
- };
40
-
41
- export const toNativeAttrValue = (propValue: any, propName: string) => {
42
- if (Array.isArray(propValue)) return propValue.join(' ');
43
- if (typeof propValue === 'boolean') return propValue;
44
-
45
- return propValue;
46
- };
47
-
48
- export const toHTMLAttrs = (props = {}) => {
49
- return Object.entries(props).reduce<{ [k: string]: string }>(
50
- (transformedProps, [propName, propValue]) => {
51
- const attrName = toNativeAttrName(propName, propValue);
52
-
53
- // Prop was stripped: don't add.
54
- if (!attrName) {
55
- return transformedProps;
56
- }
57
-
58
- const attrValue = toNativeAttrValue(propValue, propName);
59
-
60
- // Prop is undefined: omit it
61
- if (attrValue === undefined) {
62
- return transformedProps;
63
- }
64
-
65
- transformedProps[attrName] = attrValue;
66
-
67
- return transformedProps;
68
- },
69
- {},
70
- );
71
- };