Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

refactor: Attempt to improve INP performance of Vimeo player (#3027)

Ivan Starkov6c2dd4fd5c02
diff --git a/fixtures/webstudio-custom-template/README.md b/fixtures/webstudio-custom-template/README.md
index c6f8cfa5b9..0a1b6a9ca8 100644
--- a/fixtures/webstudio-custom-template/README.md
+++ b/fixtures/webstudio-custom-template/README.md
@@ -18,3 +18,12 @@ pnpm fixtures:sync
pnpm fixtures:build
# exec `pnpm run dev` to see result
```
+
+## Can be used for profiling
+
+```bash
+pnpm build
+pnpm start
+```
+
+execute chrome profiler see web vital events inside
diff --git a/fixtures/webstudio-custom-template/app/root.tsx b/fixtures/webstudio-custom-template/app/root.tsx
index 61d6aff561..d5864c8b59 100644
--- a/fixtures/webstudio-custom-template/app/root.tsx
+++ b/fixtures/webstudio-custom-template/app/root.tsx
@@ -1,13 +1,12 @@
import { Root as SdkRoot } from "@webstudio-is/react-sdk";
+import { subscribe } from "./web-vitals";
import { useEffect } from "react";
/**
* Custom root so we can add some custom logic for specific resurces etc like analytics, etc.
*/
const Root = (props: React.ComponentProps<typeof SdkRoot>) => {
- useEffect(() => {
- console.info("Hello, world!");
- }, []);
+ useEffect(subscribe, []);
return <SdkRoot {...props} />;
};
diff --git a/fixtures/webstudio-custom-template/app/web-vitals.ts b/fixtures/webstudio-custom-template/app/web-vitals.ts
new file mode 100644
index 0000000000..eeafdd4520
--- /dev/null
+++ b/fixtures/webstudio-custom-template/app/web-vitals.ts
@@ -0,0 +1,238 @@
+/**
+ * This code is based on work from the web.dev project by GoogleChrome.
+ * Source: https://github.com/GoogleChrome/web.dev/blob/a64d870f59c25431bbedf0ef06aa635072c43a2e/src/lib/analytics.js
+ * Licensed under CC BY 3.0: https://creativecommons.org/licenses/by/3.0/ https://github.com/GoogleChrome/web.dev/blob/main/LICENSE
+ * Changes were made to the original work.
+ */
+
+import {
+ onCLS,
+ onFCP,
+ onFID,
+ onINP,
+ onLCP,
+ onTTFB,
+ type CLSReportCallbackWithAttribution,
+ type FCPReportCallbackWithAttribution,
+ type FIDReportCallbackWithAttribution,
+ type INPReportCallbackWithAttribution,
+ type LCPReportCallbackWithAttribution,
+ type TTFBReportCallbackWithAttribution,
+ type ReportOpts,
+ // eslint-disable-next-line import/no-internal-modules
+} from "web-vitals/attribution";
+
+const logEvent = (
+ name: string,
+ params: {
+ /* */
+ metric_value: number;
+ }
+) => {
+ performance.measure(`${name}`, {
+ detail: JSON.stringify(params),
+ duration: params.metric_value,
+ start: performance.now(),
+ });
+
+ // eslint-disable-next-line no-console
+ console.info(JSON.stringify({ name, params }));
+};
+
+const buildElementHumanReadablePath = (element: Element) => {
+ let content =
+ element.tagName + " > " + (element?.textContent ?? "").slice(0, 100);
+
+ let node: null | Element = element;
+
+ while (node != null) {
+ content = node.tagName + " > " + content;
+ node = node.parentElement;
+ }
+
+ return content;
+};
+
+const sendToAnalitics: CLSReportCallbackWithAttribution &
+ FCPReportCallbackWithAttribution &
+ FIDReportCallbackWithAttribution &
+ INPReportCallbackWithAttribution &
+ LCPReportCallbackWithAttribution &
+ TTFBReportCallbackWithAttribution = ({
+ name,
+ value,
+ delta,
+ id,
+ attribution,
+ navigationType,
+}) => {
+ const params = {
+ event_category: "Web Vitals",
+ // The `id` value will be unique to the current page load. When sending
+ // multiple values from the same page (e.g. for CLS), Google Analytics can
+ // compute a total by grouping on this ID (note: requires `eventLabel` to
+ // be a dimension in your report).
+ event_label: id,
+ // Google Analytics metrics must be integers, so the value is rounded.
+ // For CLS the value is first multiplied by 1000 for greater precision
+ // (note: increase the multiplier for greater precision if needed).
+ value: Math.round(name === "CLS" ? delta * 1000 : delta),
+ // Send the raw metric value in addition to the value computed for GA
+ // so it's available in BigQuery and the API.
+ metric_value: value,
+ // This should already by set globally, but to ensure it's consistent
+ // with the web-vitals library, set it again.
+ // Override for 'navigational-prefetch' for the prefetch origin trial
+ // experiment (https://github.com/GoogleChrome/web.dev/pull/9532)
+ navigation_type:
+ navigationType === "navigate" &&
+ performance.getEntriesByType &&
+ performance.getEntriesByType("navigation")[0] &&
+ (
+ performance.getEntriesByType("navigation")[0] as unknown as {
+ deliveryType: string;
+ }
+ ).deliveryType === "navigational-prefetch"
+ ? "navigational-prefetch"
+ : navigationType,
+ url: location.href,
+
+ debug_target_path: undefined as unknown as string,
+ last_touched_path: undefined as unknown as string,
+ };
+
+ let overrides;
+ let debugInputDelay;
+ let debugProcessingTime;
+ let debugPresentationDelay;
+
+ switch (name) {
+ case "CLS":
+ overrides = {
+ debug_time: attribution.largestShiftTime,
+ debug_load_state: attribution.loadState,
+ debug_target: attribution.largestShiftTarget || "(not set)",
+ };
+ break;
+ case "FCP":
+ overrides = {
+ debug_ttfb: attribution.timeToFirstByte,
+ debug_fb2fcp: attribution.firstByteToFCP,
+ debug_load_state: attribution.loadState,
+ debug_target: attribution.loadState || "(not set)",
+ };
+ break;
+ case "FID":
+ overrides = {
+ debug_event: attribution.eventType,
+ debug_time: attribution.eventTime,
+ debug_load_state: attribution.loadState,
+ debug_target: attribution.eventTarget || "(not set)",
+ };
+ break;
+ case "INP":
+ if (attribution.eventEntry) {
+ debugInputDelay = Math.round(
+ attribution.eventEntry.processingStart -
+ attribution.eventEntry.startTime
+ );
+ debugProcessingTime = Math.round(
+ attribution.eventEntry.processingEnd -
+ attribution.eventEntry.processingStart
+ );
+ debugPresentationDelay = Math.round(
+ // RenderTime is an estimate, because duration is rounded, and may get rounded down.
+ // In rare cases it can be less than processingEnd and that breaks performance.measure().
+ // Lets make sure its at least 4ms in those cases so you can just barely see it.
+ Math.max(
+ attribution.eventEntry.processingEnd + 4,
+ attribution.eventEntry.startTime + attribution.eventEntry.duration
+ ) - attribution.eventEntry.processingEnd
+ );
+ }
+ overrides = {
+ debug_event: attribution.eventType,
+ debug_time: attribution.eventTime,
+ debug_load_state: attribution.loadState,
+ debug_target: attribution.eventTarget || "(not set)",
+ debug_input_delay: debugInputDelay,
+ debug_processing_time: debugProcessingTime,
+ debug_presentation_delay: debugPresentationDelay,
+ };
+ break;
+ case "LCP":
+ overrides = {
+ debug_url: attribution.url,
+ debug_ttfb: attribution.timeToFirstByte,
+ debug_rld: attribution.resourceLoadDelay,
+ debug_rlt: attribution.resourceLoadTime,
+ debug_erd: attribution.elementRenderDelay,
+ debug_target: attribution.element || "(not set)",
+ };
+ break;
+ case "TTFB":
+ overrides = {
+ debug_waiting_time: attribution.waitingTime,
+ debug_dns_time: attribution.dnsTime,
+ debug_connection_time: attribution.connectionTime,
+ debug_request_time: attribution.requestTime,
+ };
+ break;
+ }
+
+ let selector = "";
+ if ("debug_target" in overrides && overrides.debug_target !== "(not set)") {
+ try {
+ // Fix radix like selectors with `:` inside
+ selector = overrides.debug_target.replace(/([:])/g, "\\$1");
+ const element = document.querySelector(selector);
+
+ if (element) {
+ const content = buildElementHumanReadablePath(element);
+ params.debug_target_path = content;
+ }
+ } catch (e) {
+ // eslint-disable-next-line no-console
+ console.error(e);
+ // eslint-disable-next-line no-console
+ console.error(`selector: ${selector} seems like not working`);
+ }
+ }
+
+ logEvent(name, Object.assign(params, overrides, { is_mobile: _isMobile }));
+};
+
+let _subscribed = false;
+let _isMobile = false;
+
+export const subscribe = () => {
+ const reportOptions: ReportOpts = {
+ durationThreshold: 0,
+ reportAllChanges: true,
+ };
+
+ if (_subscribed === false) {
+ onCLS(sendToAnalitics, reportOptions);
+ onFCP(sendToAnalitics, reportOptions);
+ onFID(sendToAnalitics, reportOptions);
+ onINP(sendToAnalitics, reportOptions);
+ onLCP(sendToAnalitics, reportOptions);
+ onTTFB(sendToAnalitics, reportOptions);
+ }
+
+ try {
+ _isMobile = window.matchMedia("(hover: none)").matches;
+ } catch (e) {
+ // eslint-disable-next-line no-console
+ console.error(e);
+ }
+
+ const options = {
+ capture: true,
+ passive: true,
+ };
+
+ _subscribed = true;
+
+ return () => {};
+};
diff --git a/fixtures/webstudio-custom-template/custom-template-stage/app/root.tsx b/fixtures/webstudio-custom-template/custom-template-stage/app/root.tsx
index 61d6aff561..d5864c8b59 100644
--- a/fixtures/webstudio-custom-template/custom-template-stage/app/root.tsx
+++ b/fixtures/webstudio-custom-template/custom-template-stage/app/root.tsx
@@ -1,13 +1,12 @@
import { Root as SdkRoot } from "@webstudio-is/react-sdk";
+import { subscribe } from "./web-vitals";
import { useEffect } from "react";
/**
* Custom root so we can add some custom logic for specific resurces etc like analytics, etc.
*/
const Root = (props: React.ComponentProps<typeof SdkRoot>) => {
- useEffect(() => {
- console.info("Hello, world!");
- }, []);
+ useEffect(subscribe, []);
return <SdkRoot {...props} />;
};
diff --git a/fixtures/webstudio-custom-template/custom-template-stage/app/web-vitals.ts b/fixtures/webstudio-custom-template/custom-template-stage/app/web-vitals.ts
new file mode 100644
index 0000000000..eeafdd4520
--- /dev/null
+++ b/fixtures/webstudio-custom-template/custom-template-stage/app/web-vitals.ts
@@ -0,0 +1,238 @@
+/**
+ * This code is based on work from the web.dev project by GoogleChrome.
+ * Source: https://github.com/GoogleChrome/web.dev/blob/a64d870f59c25431bbedf0ef06aa635072c43a2e/src/lib/analytics.js
+ * Licensed under CC BY 3.0: https://creativecommons.org/licenses/by/3.0/ https://github.com/GoogleChrome/web.dev/blob/main/LICENSE
+ * Changes were made to the original work.
+ */
+
+import {
+ onCLS,
+ onFCP,
+ onFID,
+ onINP,
+ onLCP,
+ onTTFB,
+ type CLSReportCallbackWithAttribution,
+ type FCPReportCallbackWithAttribution,
+ type FIDReportCallbackWithAttribution,
+ type INPReportCallbackWithAttribution,
+ type LCPReportCallbackWithAttribution,
+ type TTFBReportCallbackWithAttribution,
+ type ReportOpts,
+ // eslint-disable-next-line import/no-internal-modules
+} from "web-vitals/attribution";
+
+const logEvent = (
+ name: string,
+ params: {
+ /* */
+ metric_value: number;
+ }
+) => {
+ performance.measure(`${name}`, {
+ detail: JSON.stringify(params),
+ duration: params.metric_value,
+ start: performance.now(),
+ });
+
+ // eslint-disable-next-line no-console
+ console.info(JSON.stringify({ name, params }));
+};
+
+const buildElementHumanReadablePath = (element: Element) => {
+ let content =
+ element.tagName + " > " + (element?.textContent ?? "").slice(0, 100);
+
+ let node: null | Element = element;
+
+ while (node != null) {
+ content = node.tagName + " > " + content;
+ node = node.parentElement;
+ }
+
+ return content;
+};
+
+const sendToAnalitics: CLSReportCallbackWithAttribution &
+ FCPReportCallbackWithAttribution &
+ FIDReportCallbackWithAttribution &
+ INPReportCallbackWithAttribution &
+ LCPReportCallbackWithAttribution &
+ TTFBReportCallbackWithAttribution = ({
+ name,
+ value,
+ delta,
+ id,
+ attribution,
+ navigationType,
+}) => {
+ const params = {
+ event_category: "Web Vitals",
+ // The `id` value will be unique to the current page load. When sending
+ // multiple values from the same page (e.g. for CLS), Google Analytics can
+ // compute a total by grouping on this ID (note: requires `eventLabel` to
+ // be a dimension in your report).
+ event_label: id,
+ // Google Analytics metrics must be integers, so the value is rounded.
+ // For CLS the value is first multiplied by 1000 for greater precision
+ // (note: increase the multiplier for greater precision if needed).
+ value: Math.round(name === "CLS" ? delta * 1000 : delta),
+ // Send the raw metric value in addition to the value computed for GA
+ // so it's available in BigQuery and the API.
+ metric_value: value,
+ // This should already by set globally, but to ensure it's consistent
+ // with the web-vitals library, set it again.
+ // Override for 'navigational-prefetch' for the prefetch origin trial
+ // experiment (https://github.com/GoogleChrome/web.dev/pull/9532)
+ navigation_type:
+ navigationType === "navigate" &&
+ performance.getEntriesByType &&
+ performance.getEntriesByType("navigation")[0] &&
+ (
+ performance.getEntriesByType("navigation")[0] as unknown as {
+ deliveryType: string;
+ }
+ ).deliveryType === "navigational-prefetch"
+ ? "navigational-prefetch"
+ : navigationType,
+ url: location.href,
+
+ debug_target_path: undefined as unknown as string,
+ last_touched_path: undefined as unknown as string,
+ };
+
+ let overrides;
+ let debugInputDelay;
+ let debugProcessingTime;
+ let debugPresentationDelay;
+
+ switch (name) {
+ case "CLS":
+ overrides = {
+ debug_time: attribution.largestShiftTime,
+ debug_load_state: attribution.loadState,
+ debug_target: attribution.largestShiftTarget || "(not set)",
+ };
+ break;
+ case "FCP":
+ overrides = {
+ debug_ttfb: attribution.timeToFirstByte,
+ debug_fb2fcp: attribution.firstByteToFCP,
+ debug_load_state: attribution.loadState,
+ debug_target: attribution.loadState || "(not set)",
+ };
+ break;
+ case "FID":
+ overrides = {
+ debug_event: attribution.eventType,
+ debug_time: attribution.eventTime,
+ debug_load_state: attribution.loadState,
+ debug_target: attribution.eventTarget || "(not set)",
+ };
+ break;
+ case "INP":
+ if (attribution.eventEntry) {
+ debugInputDelay = Math.round(
+ attribution.eventEntry.processingStart -
+ attribution.eventEntry.startTime
+ );
+ debugProcessingTime = Math.round(
+ attribution.eventEntry.processingEnd -
+ attribution.eventEntry.processingStart
+ );
+ debugPresentationDelay = Math.round(
+ // RenderTime is an estimate, because duration is rounded, and may get rounded down.
+ // In rare cases it can be less than processingEnd and that breaks performance.measure().
+ // Lets make sure its at least 4ms in those cases so you can just barely see it.
+ Math.max(
+ attribution.eventEntry.processingEnd + 4,
+ attribution.eventEntry.startTime + attribution.eventEntry.duration
+ ) - attribution.eventEntry.processingEnd
+ );
+ }
+ overrides = {
+ debug_event: attribution.eventType,
+ debug_time: attribution.eventTime,
+ debug_load_state: attribution.loadState,
+ debug_target: attribution.eventTarget || "(not set)",
+ debug_input_delay: debugInputDelay,
+ debug_processing_time: debugProcessingTime,
+ debug_presentation_delay: debugPresentationDelay,
+ };
+ break;
+ case "LCP":
+ overrides = {
+ debug_url: attribution.url,
+ debug_ttfb: attribution.timeToFirstByte,
+ debug_rld: attribution.resourceLoadDelay,
+ debug_rlt: attribution.resourceLoadTime,
+ debug_erd: attribution.elementRenderDelay,
+ debug_target: attribution.element || "(not set)",
+ };
+ break;
+ case "TTFB":
+ overrides = {
+ debug_waiting_time: attribution.waitingTime,
+ debug_dns_time: attribution.dnsTime,
+ debug_connection_time: attribution.connectionTime,
+ debug_request_time: attribution.requestTime,
+ };
+ break;
+ }
+
+ let selector = "";
+ if ("debug_target" in overrides && overrides.debug_target !== "(not set)") {
+ try {
+ // Fix radix like selectors with `:` inside
+ selector = overrides.debug_target.replace(/([:])/g, "\\$1");
+ const element = document.querySelector(selector);
+
+ if (element) {
+ const content = buildElementHumanReadablePath(element);
+ params.debug_target_path = content;
+ }
+ } catch (e) {
+ // eslint-disable-next-line no-console
+ console.error(e);
+ // eslint-disable-next-line no-console
+ console.error(`selector: ${selector} seems like not working`);
+ }
+ }
+
+ logEvent(name, Object.assign(params, overrides, { is_mobile: _isMobile }));
+};
+
+let _subscribed = false;
+let _isMobile = false;
+
+export const subscribe = () => {
+ const reportOptions: ReportOpts = {
+ durationThreshold: 0,
+ reportAllChanges: true,
+ };
+
+ if (_subscribed === false) {
+ onCLS(sendToAnalitics, reportOptions);
+ onFCP(sendToAnalitics, reportOptions);
+ onFID(sendToAnalitics, reportOptions);
+ onINP(sendToAnalitics, reportOptions);
+ onLCP(sendToAnalitics, reportOptions);
+ onTTFB(sendToAnalitics, reportOptions);
+ }
+
+ try {
+ _isMobile = window.matchMedia("(hover: none)").matches;
+ } catch (e) {
+ // eslint-disable-next-line no-console
+ console.error(e);
+ }
+
+ const options = {
+ capture: true,
+ passive: true,
+ };
+
+ _subscribed = true;
+
+ return () => {};
+};
diff --git a/fixtures/webstudio-custom-template/package.json b/fixtures/webstudio-custom-template/package.json
index bb7ef17212..4bbc046dba 100644
--- a/fixtures/webstudio-custom-template/package.json
+++ b/fixtures/webstudio-custom-template/package.json
@@ -28,7 +28,8 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"@size-limit/file": "^9.0.0",
- "size-limit": "^9.0.0"
+ "size-limit": "^9.0.0",
+ "web-vitals": "^3.5.2"
},
"devDependencies": {
"@remix-run/serve": "^1.19.2",
@@ -44,7 +45,7 @@
"size-limit": [
{
"path": "public/build/*.js",
- "limit": "137 kB",
+ "limit": "160 kB",
"gzip": false
}
],
diff --git a/packages/sdk-components-react/package.json b/packages/sdk-components-react/package.json
index ff407facfa..de85a86c6b 100644
--- a/packages/sdk-components-react/package.json
+++ b/packages/sdk-components-react/package.json
@@ -39,7 +39,7 @@
"dts": "tsc --project tsconfig.dts.json",
"typecheck": "tsc",
"checks": "pnpm typecheck",
- "storybook:dev": "storybook dev -p 6006",
+ "storybook:dev": "storybook dev -p 6006 --static-dir=./public",
"storybook:build": "storybook build"
},
"peerDependencies": {
diff --git a/packages/sdk-components-react/public/home_wsKvRSqvkajPPBeycZ-C8.svg b/packages/sdk-components-react/public/home_wsKvRSqvkajPPBeycZ-C8.svg
new file mode 100644
index 0000000000..67284862e7
--- /dev/null
+++ b/packages/sdk-components-react/public/home_wsKvRSqvkajPPBeycZ-C8.svg
@@ -0,0 +1,3 @@
+<svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
+<path fill-rule="evenodd" clip-rule="evenodd" d="M7.6559 0.907089C7.88124 0.846075 8.11876 0.846075 8.34411 0.907089C8.60553 0.97787 8.82581 1.15075 9.00433 1.29086C9.02124 1.30414 9.03777 1.31712 9.05393 1.32968L13.5755 4.84644C13.5927 4.85983 13.6098 4.87308 13.6267 4.88623C13.8784 5.08159 14.0978 5.25187 14.2612 5.47281C14.4047 5.66677 14.5116 5.88528 14.5766 6.11759C14.6507 6.38223 14.6504 6.65995 14.65 6.9786C14.65 7.00004 14.65 7.02165 14.65 7.04347V11.8919C14.65 12.2438 14.65 12.5464 14.6297 12.7954C14.6082 13.0579 14.561 13.3177 14.4338 13.5672C14.2437 13.9404 13.9403 14.2438 13.5671 14.4339C13.3176 14.561 13.0579 14.6083 12.7953 14.6298C12.5463 14.6501 12.2437 14.6501 11.8918 14.6501H4.10816C3.75628 14.6501 3.4537 14.6501 3.20467 14.6298C2.94214 14.6083 2.68243 14.561 2.43292 14.4339C2.05973 14.2438 1.75632 13.9404 1.56617 13.5672C1.43904 13.3177 1.39178 13.0579 1.37033 12.7954C1.34998 12.5464 1.34999 12.2438 1.35 11.8919L1.35 7.04347C1.35 7.02165 1.34998 7.00003 1.34996 6.9786C1.34963 6.65995 1.34934 6.38222 1.42341 6.11759C1.48844 5.88527 1.59531 5.66677 1.73876 5.47281C1.90218 5.25187 2.12158 5.08159 2.3733 4.88623C2.39024 4.87308 2.40732 4.85983 2.42453 4.84644L6.94607 1.32968C6.96223 1.31711 6.97877 1.30414 6.99568 1.29086C7.1742 1.15075 7.39448 0.97787 7.6559 0.907089ZM6.65 13.3501H9.35V9.06675C9.35 8.86934 9.3495 8.76038 9.34308 8.68182C9.34236 8.67303 9.34162 8.66552 9.34091 8.65918C9.33457 8.65847 9.32706 8.65773 9.31827 8.65701C9.23971 8.65059 9.13075 8.65009 8.93334 8.65009H7.06667C6.86926 8.65009 6.76029 8.65059 6.68174 8.65701C6.67294 8.65773 6.66544 8.65847 6.6591 8.65918C6.65839 8.66552 6.65765 8.67303 6.65693 8.68182C6.65051 8.76038 6.65 8.86934 6.65 9.06675V13.3501ZM10.65 13.3501L10.65 9.04584C10.65 8.87727 10.65 8.71403 10.6388 8.57596C10.6264 8.42459 10.5973 8.24721 10.5065 8.069C10.3803 7.82125 10.1788 7.61983 9.93109 7.49359C9.75288 7.40279 9.5755 7.37369 9.42413 7.36133C9.28606 7.35005 9.12282 7.35007 8.95425 7.35008H7.04575C6.87718 7.35007 6.71395 7.35005 6.57587 7.36133C6.42451 7.37369 6.24712 7.40279 6.06892 7.49359C5.82117 7.61983 5.61974 7.82125 5.49351 8.069C5.40271 8.24721 5.37361 8.42459 5.36124 8.57596C5.34996 8.71403 5.34998 8.87727 5.35 9.04584L5.35 13.3501H4.13334C3.74924 13.3501 3.50026 13.3496 3.31054 13.3341C3.1284 13.3192 3.05881 13.2938 3.02311 13.2756C2.89453 13.2101 2.79 13.1056 2.72448 12.977C2.70629 12.9413 2.68089 12.8717 2.66601 12.6896C2.65051 12.4998 2.65 12.2508 2.65 11.8668V7.04347C2.65 6.62375 2.65565 6.53819 2.6753 6.46801C2.6977 6.38796 2.73452 6.31268 2.78395 6.24586C2.82729 6.18726 2.89135 6.13027 3.22266 5.87259L7.74419 2.35584C7.86861 2.25907 7.93516 2.20781 7.98588 2.17392C7.99132 2.17029 7.99601 2.16725 8 2.16473C8.00399 2.16725 8.00869 2.17029 8.01413 2.17392C8.06485 2.20781 8.13139 2.25907 8.25581 2.35584L12.7773 5.87259C13.1087 6.13027 13.1727 6.18726 13.2161 6.24586C13.2655 6.31268 13.3023 6.38796 13.3247 6.46801C13.3444 6.53819 13.35 6.62375 13.35 7.04347V11.8668C13.35 12.2508 13.3495 12.4998 13.334 12.6896C13.3191 12.8717 13.2937 12.9413 13.2755 12.977C13.21 13.1056 13.1055 13.2101 12.9769 13.2756C12.9412 13.2938 12.8716 13.3192 12.6895 13.3341C12.4997 13.3496 12.2508 13.3501 11.8667 13.3501H10.65Z" fill="#11181C"/>
+</svg>
diff --git a/packages/sdk-components-react/src/vimeo.stories.tsx b/packages/sdk-components-react/src/vimeo.stories.tsx
index 610ce0ba93..785648b29e 100644
--- a/packages/sdk-components-react/src/vimeo.stories.tsx
+++ b/packages/sdk-components-react/src/vimeo.stories.tsx
@@ -1,13 +1,440 @@
-import type { Meta, StoryObj } from "@storybook/react";
-import { Vimeo as VimeoPrimitive } from "./vimeo";
+import { VimeoPreviewImage } from "./vimeo-preview-image";
+import { Box } from "./box";
+import { HtmlEmbed } from "./html-embed";
+import { VimeoPlayButton } from "./vimeo-play-button";
+import { Vimeo } from "./vimeo";
-export default {
- title: "Components/Vimeo",
- component: VimeoPrimitive,
-} satisfies Meta<typeof VimeoPrimitive>;
+const Component = () => (
+ <Vimeo
+ data-ws-id="ZkDuD4HlHP3pDdp0SXJuh"
+ data-ws-component="Vimeo"
+ url={"https://player.vimeo.com/video/831343124"}
+ showPreview={true}
+ className="ca1m5u0 ciuvvr7 cuk1bdz"
+ >
+ <VimeoPreviewImage
+ data-ws-id="wxd8Wul8dl2yPRFFedNn6"
+ data-ws-component="VimeoPreviewImage"
+ alt={"Vimeo video preview image"}
+ sizes={"100vw"}
+ src={"/home_wsKvRSqvkajPPBeycZ-C8.svg"}
+ className="c1eccbi0 cc32szi cuk1bdz c1ge5ofh c1fwh0y5 ch160p4 c1jxoq3x cewch87 c1la265j"
+ />
+ <Box
+ data-ws-id="CnbJrVYAMQ7aDqemVdWI5"
+ data-ws-component="Box"
+ className="c1eccbi0 ce5jzw0 cq3eebu c1319rdz c1x7j4n5 c176tfq4 c1qg633k"
+ >
+ <HtmlEmbed
+ data-ws-id="JeL4m1QcrRyK7gPyN3p5j"
+ data-ws-component="HtmlEmbed"
+ code={
+ '<svg xmlns="http://www.w3.org/2000/svg" id="e2CRglijn891" shape-rendering="geometricPrecision" text-rendering="geometricPrecision" viewBox="0 0 128 128" fill="currentColor" width="100%" height="100%" style="display: block;"><style>@keyframes e2CRglijn892_tr__tr{0%{transform:translate(64px,64px) rotate(90deg);animation-timing-function:cubic-bezier(.42,0,.58,1)}50%{transform:translate(64px,64px) rotate(810deg);animation-timing-function:cubic-bezier(.42,0,.58,1)}to{transform:translate(64px,64px) rotate(1530deg)}}@keyframes e2CRglijn892_s_p{0%,to{stroke:#39fbbb}25%{stroke:#4a4efa}50%{stroke:#e63cfe}75%{stroke:#ffae3c}}@keyframes e2CRglijn892_s_do{0%{stroke-dashoffset:251.89}2.5%,52.5%{stroke-dashoffset:263.88;animation-timing-function:cubic-bezier(.42,0,.58,1)}25%,75%{stroke-dashoffset:131.945}to{stroke-dashoffset:251.885909}}#e2CRglijn892_tr{animation:e2CRglijn892_tr__tr 3000ms linear infinite normal forwards}#e2CRglijn892{animation-name:e2CRglijn892_s_p,e2CRglijn892_s_do;animation-duration:3000ms;animation-fill-mode:forwards;animation-timing-function:linear;animation-direction:normal;animation-iteration-count:infinite}</style><g id="e2CRglijn892_tr" transform="translate(64,64) rotate(90)"><circle id="e2CRglijn892" r="42" fill="none" stroke="#39fbbb" stroke-dasharray="263.89" stroke-dashoffset="251.89" stroke-linecap="round" stroke-width="16" transform="scale(-1,1) translate(0,0)"/></g></svg>'
+ }
+ />
+ </Box>
+ <VimeoPlayButton
+ data-ws-id="9hBBPGSf7hB30ZkSHKjNd"
+ data-ws-component="VimeoPlayButton"
+ aria-label={"Play button"}
+ className="c1eccbi0 c1yx3ait c1scl4ay ce5jzw0 cq3eebu cxar3by c1fiqkhd c1b095zn cyw79s9 c2f7s8e c9bs64c c6lou2r c16c73ai c1fisnnh c82rye8 c1xaktbd c1xplc5b c5admkk ccn3fhu cyn2bny c1xs2zvh c1be0m8p"
+ >
+ <Box
+ data-ws-id="D__QElBIIQtamhJN3a4FI"
+ data-ws-component="Box"
+ aria-hidden={"true"}
+ className="c14af118 c1xa5m0w"
+ >
+ <HtmlEmbed
+ data-ws-id="iEc6hab-WardXZc5P9wJu"
+ data-ws-component="HtmlEmbed"
+ code={
+ '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 22 22" fill="currentColor" width="100%" height="100%" style="display: block;"><path d="M4.766 5.765c0-.725 0-1.088.178-1.288a.93.93 0 0 1 .648-.294c.294-.015.65.186 1.359.588l9.234 5.235c.586.332.88.498.982.708.09.183.09.389 0 .572-.102.21-.396.376-.982.708l-9.234 5.235c-.71.402-1.065.603-1.359.588a.93.93 0 0 1-.648-.294c-.178-.2-.178-.563-.178-1.288V5.765Z"/></svg>'
+ }
+ />
+ </Box>
+ </VimeoPlayButton>
+ </Vimeo>
+);
-export const Vimeo: StoryObj<typeof VimeoPrimitive> = {
- args: {
- style: { minHeight: 20, outline: "1px solid black" },
+const Story = {
+ render() {
+ return (
+ <>
+ <style>
+ {`
+html {
+ margin: 0;
+ display: grid;
+ min-height: 100%;
+}
+@media all {
+ body:where([data-ws-component="Body"]) {
+ margin-top: 0;
+ margin-right: 0;
+ margin-bottom: 0;
+ margin-left: 0;
+ font-family: Arial, Roboto, sans-serif;
+ font-size: 16px;
+ line-height: 1.2;
+ box-sizing: border-box;
+ border-top-width: 1px;
+ border-right-width: 1px;
+ border-bottom-width: 1px;
+ border-left-width: 1px;
+ -webkit-font-smoothing: antialiased;
+ -moz-osx-font-smoothing: grayscale;
+ }
+ h1:where([data-ws-component="Heading"]) {
+ box-sizing: border-box;
+ border-top-width: 1px;
+ border-right-width: 1px;
+ border-bottom-width: 1px;
+ border-left-width: 1px;
+ outline-width: 1px;
+ }
+ h2:where([data-ws-component="Heading"]) {
+ box-sizing: border-box;
+ border-top-width: 1px;
+ border-right-width: 1px;
+ border-bottom-width: 1px;
+ border-left-width: 1px;
+ outline-width: 1px;
+ }
+ h3:where([data-ws-component="Heading"]) {
+ box-sizing: border-box;
+ border-top-width: 1px;
+ border-right-width: 1px;
+ border-bottom-width: 1px;
+ border-left-width: 1px;
+ outline-width: 1px;
+ }
+ h4:where([data-ws-component="Heading"]) {
+ box-sizing: border-box;
+ border-top-width: 1px;
+ border-right-width: 1px;
+ border-bottom-width: 1px;
+ border-left-width: 1px;
+ outline-width: 1px;
+ }
+ h5:where([data-ws-component="Heading"]) {
+ box-sizing: border-box;
+ border-top-width: 1px;
+ border-right-width: 1px;
+ border-bottom-width: 1px;
+ border-left-width: 1px;
+ outline-width: 1px;
+ }
+ h6:where([data-ws-component="Heading"]) {
+ box-sizing: border-box;
+ border-top-width: 1px;
+ border-right-width: 1px;
+ border-bottom-width: 1px;
+ border-left-width: 1px;
+ outline-width: 1px;
+ }
+ a:where([data-ws-component="Link"]) {
+ box-sizing: border-box;
+ border-top-width: 1px;
+ border-right-width: 1px;
+ border-bottom-width: 1px;
+ border-left-width: 1px;
+ outline-width: 1px;
+ min-height: 1em;
+ display: inline-block;
+ }
+ div:where([data-ws-component="Vimeo"]) {
+ box-sizing: border-box;
+ border-top-width: 1px;
+ border-right-width: 1px;
+ border-bottom-width: 1px;
+ border-left-width: 1px;
+ outline-width: 1px;
+ }
+ img:where([data-ws-component="VimeoPreviewImage"]) {
+ box-sizing: border-box;
+ border-top-width: 1px;
+ border-right-width: 1px;
+ border-bottom-width: 1px;
+ border-left-width: 1px;
+ outline-width: 1px;
+ max-width: 100%;
+ display: block;
+ }
+ div:where([data-ws-component="Box"]) {
+ box-sizing: border-box;
+ border-top-width: 1px;
+ border-right-width: 1px;
+ border-bottom-width: 1px;
+ border-left-width: 1px;
+ outline-width: 1px;
+ }
+ address:where([data-ws-component="Box"]) {
+ box-sizing: border-box;
+ border-top-width: 1px;
+ border-right-width: 1px;
+ border-bottom-width: 1px;
+ border-left-width: 1px;
+ outline-width: 1px;
+ }
+ article:where([data-ws-component="Box"]) {
+ box-sizing: border-box;
+ border-top-width: 1px;
+ border-right-width: 1px;
+ border-bottom-width: 1px;
+ border-left-width: 1px;
+ outline-width: 1px;
+ }
+ aside:where([data-ws-component="Box"]) {
+ box-sizing: border-box;
+ border-top-width: 1px;
+ border-right-width: 1px;
+ border-bottom-width: 1px;
+ border-left-width: 1px;
+ outline-width: 1px;
+ }
+ figure:where([data-ws-component="Box"]) {
+ box-sizing: border-box;
+ border-top-width: 1px;
+ border-right-width: 1px;
+ border-bottom-width: 1px;
+ border-left-width: 1px;
+ outline-width: 1px;
+ }
+ footer:where([data-ws-component="Box"]) {
+ box-sizing: border-box;
+ border-top-width: 1px;
+ border-right-width: 1px;
+ border-bottom-width: 1px;
+ border-left-width: 1px;
+ outline-width: 1px;
+ }
+ header:where([data-ws-component="Box"]) {
+ box-sizing: border-box;
+ border-top-width: 1px;
+ border-right-width: 1px;
+ border-bottom-width: 1px;
+ border-left-width: 1px;
+ outline-width: 1px;
+ }
+ main:where([data-ws-component="Box"]) {
+ box-sizing: border-box;
+ border-top-width: 1px;
+ border-right-width: 1px;
+ border-bottom-width: 1px;
+ border-left-width: 1px;
+ outline-width: 1px;
+ }
+ nav:where([data-ws-component="Box"]) {
+ box-sizing: border-box;
+ border-top-width: 1px;
+ border-right-width: 1px;
+ border-bottom-width: 1px;
+ border-left-width: 1px;
+ outline-width: 1px;
+ }
+ section:where([data-ws-component="Box"]) {
+ box-sizing: border-box;
+ border-top-width: 1px;
+ border-right-width: 1px;
+ border-bottom-width: 1px;
+ border-left-width: 1px;
+ outline-width: 1px;
+ }
+ button:where([data-ws-component="VimeoPlayButton"]) {
+ font-family: inherit;
+ font-size: 100%;
+ line-height: 1.15;
+ margin-top: 0;
+ margin-right: 0;
+ margin-bottom: 0;
+ margin-left: 0;
+ box-sizing: border-box;
+ border-top-width: 1px;
+ border-right-width: 1px;
+ border-bottom-width: 1px;
+ border-left-width: 1px;
+ border-top-style: solid;
+ border-right-style: solid;
+ border-bottom-style: solid;
+ border-left-style: solid;
+ text-transform: none;
+ }
+}
+@media all {
+ .c11x2bo2 {
+ font-size: 4em;
+ }
+ .cykwawo {
+ font-family: "Cormorant Garamond", sans-serif;
+ }
+ .c1qmshyz {
+ background-attachment: scroll, scroll, scroll;
+ }
+ .c12wnxll {
+ -webkit-background-clip: border-box, border-box, border-box;
+ }
+ .c1hzshzo {
+ background-blend-mode: normal, normal, normal;
+ }
+ .c136ve95 {
+ background-image: linear-gradient(
+ rgba(255, 0, 0, 0.2),
+ rgba(0, 255, 255, 0.2)
+ ),
+ url("/home_wsKvRSqvkajPPBeycZ-C8.svg"),
+ linear-gradient(to right, rgba(255, 0, 255, 0.2), rgba(0, 255, 0, 0.2));
+ }
+ .cyttr60 {
+ background-origin: padding-box, padding-box, padding-box;
+ }
+ .c1cywb85 {
+ background-position: 0%, 0%, 0%;
+ }
+ .c187ui6a {
+ background-repeat: repeat, repeat, repeat;
+ }
+ .c1azrtl7 {
+ background-size: auto, auto, auto;
+ }
+ .c1vynykc {
+ margin-top: 15px;
+ }
+ .c122cnm9 {
+ margin-bottom: 15px;
+ }
+ .c15qd3jj {
+ margin-right: 15px;
+ }
+ .c1jp5sfs {
+ margin-left: 15px;
+ }
+ .ca1m5u0 {
+ position: relative;
+ }
+ .ciuvvr7 {
+ aspect-ratio: 640/360;
+ }
+ .cuk1bdz {
+ width: 100%;
+ }
+ .c1eccbi0 {
+ position: absolute;
+ }
+ .cc32szi {
+ object-fit: cover;
+ }
+ .c1ge5ofh {
+ height: 100%;
+ }
+ .c1fwh0y5 {
+ border-top-left-radius: 20px;
+ }
+ .ch160p4 {
+ border-top-right-radius: 20px;
+ }
+ .c1jxoq3x {
+ border-bottom-left-radius: 20px;
+ }
+ .cewch87 {
+ border-bottom-right-radius: 20px;
+ }
+ .c1la265j {
+ object-position: cover;
+ }
+ .ce5jzw0 {
+ top: 50%;
+ }
+ .cq3eebu {
+ left: 50%;
+ }
+ .c1319rdz {
+ width: 70px;
+ }
+ .c1x7j4n5 {
+ height: 70px;
+ }
+ .c176tfq4 {
+ margin-top: -35px;
+ }
+ .c1qg633k {
+ margin-left: -35px;
+ }
+ .c1yx3ait {
+ width: 140px;
+ }
+ .c1scl4ay {
+ height: 80px;
+ }
+ .cxar3by {
+ margin-top: -40px;
+ }
+ .c1fiqkhd {
+ margin-left: -70px;
+ }
+ .c1b095zn {
+ display: flex;
+ }
+ .cyw79s9 {
+ align-items: center;
+ }
+ .c2f7s8e {
+ justify-content: center;
+ }
+ .c9bs64c {
+ border-top-style: none;
+ }
+ .c6lou2r {
+ border-right-style: none;
+ }
+ .c16c73ai {
+ border-bottom-style: none;
+ }
+ .c1fisnnh {
+ border-left-style: none;
+ }
+ .c82rye8 {
+ border-top-left-radius: 5px;
+ }
+ .c1xaktbd {
+ border-top-right-radius: 5px;
+ }
+ .c1xplc5b {
+ border-bottom-left-radius: 5px;
+ }
+ .c5admkk {
+ border-bottom-right-radius: 5px;
+ }
+ .ccn3fhu {
+ cursor: pointer;
+ }
+ .cyn2bny {
+ background-color: rgba(18, 18, 18, 1);
+ }
+ .c1xs2zvh {
+ color: rgba(255, 255, 255, 1);
+ }
+ .c1be0m8p:hover {
+ background-color: rgba(0, 173, 239, 1);
+ }
+ .c14af118 {
+ width: 60px;
+ }
+ .c1xa5m0w {
+ height: 60px;
+ }
+}
+
+ `}
+ </style>
+ <Component />
+ </>
+ );
},
};
+
+export { Story as Vimeo };
+
+export default {
+ title: "Components/Vimeo",
+};
diff --git a/packages/sdk-components-react/src/vimeo.tsx b/packages/sdk-components-react/src/vimeo.tsx
index aa8d43cc0a..be62b2c782 100644
--- a/packages/sdk-components-react/src/vimeo.tsx
+++ b/packages/sdk-components-react/src/vimeo.tsx
@@ -16,6 +16,7 @@ import {
createContext,
type ContextType,
useMemo,
+ useCallback,
} from "react";
import { ReactSdkContext } from "@webstudio-is/react-sdk";
import { shallowEqual } from "shallow-equal";
@@ -135,6 +136,12 @@ const warmConnections = () => {
if (warmed) {
return;
}
+
+ if (window.matchMedia("(hover: none)").matches) {
+ // Useless on touch devices
+ return;
+ }
+
preconnect(PLAYER_CDN);
preconnect(IFRAME_CDN);
preconnect(IMAGE_CDN);
@@ -233,6 +240,8 @@ const useVimeo = ({
const [playerStatus, setPlayerStatus] = useState<PlayerStatus>("initial");
const elementRef = useRef<ElementRef<typeof defaultTag> | null>(null);
const [previewImageUrl, setPreviewImageUrl] = useState<URL>();
+ const [isPending, setIsPending] = useState(false);
+ const loadPreviewOnceRef = useRef(false);
useEffect(() => {
setPlayerStatus(
@@ -248,10 +257,19 @@ const useVimeo = ({
) {
return;
}
+
if (showPreview) {
- loadPreviewImage(elementRef.current, options.url).then(
- setPreviewImageUrl
- );
+ if (loadPreviewOnceRef.current) {
+ return;
+ }
+
+ loadPreviewOnceRef.current = true;
+
+ loadPreviewImage(elementRef.current, options.url)
+ .then(setPreviewImageUrl)
+ .catch(() => {
+ /* DO NOTHING */
+ });
return;
}
setPreviewImageUrl(undefined);
@@ -266,14 +284,35 @@ const useVimeo = ({
}, [options]);
useEffect(() => {
- if (elementRef.current === null || playerStatus === "initial") {
+ if (isPending) {
+ setPlayerStatus((status) =>
+ status === "initial" ? "initialized" : status
+ );
+ }
+ }, [isPending]);
+
+ const toggleCreatePlayer = playerStatus !== "initial";
+
+ useEffect(() => {
+ if (elementRef.current === null) {
+ return;
+ }
+ if (toggleCreatePlayer === false) {
return;
}
+
return createPlayer(elementRef.current, stableOptions, { loading }, () => {
setPlayerStatus("ready");
});
- }, [stableOptions, playerStatus, loading]);
- return { previewImageUrl, playerStatus, setPlayerStatus, elementRef };
+ }, [stableOptions, toggleCreatePlayer, loading]);
+
+ const start = useCallback(() => {
+ setIsPending(true);
+ }, []);
+
+ const status = isPending ? "initialized" : playerStatus;
+
+ return { previewImageUrl, status, start, elementRef };
};
export type VimeoOptions = Omit<
@@ -347,43 +386,42 @@ export const Vimeo = forwardRef<Ref, Props>(
ref
) => {
const { renderer } = useContext(ReactSdkContext);
- const { previewImageUrl, playerStatus, setPlayerStatus, elementRef } =
- useVimeo({
- renderer,
- showPreview,
- loading,
- options: {
- url,
- autoplay,
- autopause,
- keyboard,
- loop,
- muted,
- pip,
- playsinline,
- quality,
- responsive,
- speed,
- transparent,
- portrait: showPortrait,
- byline: showByline,
- title: showTitle,
- color: controlsColor,
- controls: showControls,
- interactive_params: interactiveParams,
- background: backgroundMode,
- dnt: doNotTrack,
- },
- });
+ const { previewImageUrl, status, start, elementRef } = useVimeo({
+ renderer,
+ showPreview,
+ loading,
+ options: {
+ url,
+ autoplay,
+ autopause,
+ keyboard,
+ loop,
+ muted,
+ pip,
+ playsinline,
+ quality,
+ responsive,
+ speed,
+ transparent,
+ portrait: showPortrait,
+ byline: showByline,
+ title: showTitle,
+ color: controlsColor,
+ controls: showControls,
+ interactive_params: interactiveParams,
+ background: backgroundMode,
+ dnt: doNotTrack,
+ },
+ });
return (
<VimeoContext.Provider
value={{
- status: playerStatus,
+ status,
previewImageUrl,
onInitPlayer() {
if (renderer !== "canvas") {
- setPlayerStatus("initialized");
+ start();
}
},
}}
diff --git a/packages/sdk-components-react/src/vimeo.ws.ts b/packages/sdk-components-react/src/vimeo.ws.ts
index 90f401e479..fe5f88c2b7 100644
--- a/packages/sdk-components-react/src/vimeo.ws.ts
+++ b/packages/sdk-components-react/src/vimeo.ws.ts
@@ -101,7 +101,7 @@ export const meta: WsComponentMeta = {
},
{
type: "instance",
- component: "Box",
+ component: "VimeoSpinner",
label: "Spinner",
styles: [
{
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index bd56f0f55a..7f55bc6f93 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -485,6 +485,9 @@ importers:
size-limit:
specifier: ^9.0.0
version: 9.0.0
+ web-vitals:
+ specifier: ^3.5.2
+ version: 3.5.2
devDependencies:
'@remix-run/dev':
specifier: ^1.19.2
@@ -17458,6 +17461,10 @@ packages:
engines: {node: '>= 14'}
dev: false
+ resolution: {integrity: sha512-c0rhqNcHXRkY/ogGDJQxZ9Im9D19hDihbzSQJrsioex+KnFgmMzBiy57Z1EjkhX/+OjyBpclDCzz2ITtjokFmg==}
+ dev: false
+
resolution: {integrity: sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==}