@dotcms/experiments 0.0.1-alpha.13 → 0.0.1-alpha.14

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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dotcms/experiments",
3
- "version": "0.0.1-alpha.13",
3
+ "version": "0.0.1-alpha.14",
4
4
  "description": "Official JavaScript library to use Experiments with DotCMS.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -25,7 +25,7 @@
25
25
  "peerDependencies": {
26
26
  "react": ">=18",
27
27
  "react-dom": ">=18",
28
- "@dotcms/client": "0.0.1-alpha.13"
28
+ "@dotcms/client": "0.0.1-alpha.14"
29
29
  },
30
30
  "module": "./index.esm.js",
31
31
  "type": "module",
package/src/index.d.ts CHANGED
@@ -1,3 +1 @@
1
- export * from './lib/hooks/useExperiments';
2
- export * from './lib/components/DotExperimentsProvider';
3
- export * from './lib/contexts/DotExperimentsContext';
1
+ export * from './lib/components/withExperiments';
@@ -0,0 +1,21 @@
1
+ /// <reference types="react" />
2
+ import { DotcmsPageProps } from '@dotcms/react';
3
+ interface ExperimentHandlingProps extends DotcmsPageProps {
4
+ WrappedComponent: React.ComponentType<DotcmsPageProps>;
5
+ }
6
+ /**
7
+ * A React functional component that conditionally renders a WrappedComponent based on the
8
+ * experiment variant state. It uses the `useExperimentVariant` hook to determine if there's a
9
+ * variant mismatch. If the current variant does not match the assigned variant, it temporarily
10
+ * hides the WrappedComponent by rendering it with `visibility: hidden`. Once the correct variant
11
+ * is confirmed, it renders the WrappedComponent normally.
12
+ *
13
+ * @param {React.ComponentType<DotcmsPageProps>} WrappedComponent - The React component that will be
14
+ * conditionally rendered based on the experiment variant.
15
+ * @param {DotcmsPageProps} props - Props expected by the WrappedComponent, along with any additional
16
+ * props that extend from DotcmsPageProps.
17
+ * @returns {React.ReactElement} A React element that either renders the WrappedComponent hidden or visible
18
+ * based on the experiment variant.
19
+ */
20
+ export declare const DotExperimentHandlingComponent: React.FC<ExperimentHandlingProps>;
21
+ export {};
@@ -0,0 +1,20 @@
1
+ import React, { ReactNode } from 'react';
2
+ import { DotcmsPageProps } from '@dotcms/react';
3
+ import { DotExperimentConfig } from '../shared/models';
4
+ export interface PageProviderProps {
5
+ readonly entity: any;
6
+ readonly children: ReactNode;
7
+ }
8
+ /**
9
+ * Wraps a given component with experiment handling capabilities using the 'useExperimentVariant' hook.
10
+ * This HOC checks if the entity's assigned experiment variant differs from the currently displayed variant.
11
+ * If they differ, the content is hidden until the correct variant is displayed. Once the assigned variant
12
+ * matches the displayed variant, the content of the WrappedComponent is shown.
13
+ *
14
+ * @param {React.ComponentType<DotcmsPageProps>} WrappedComponent - The component to be enhanced.
15
+ * @param {DotExperimentConfig} config - Configuration for experiment handling, including any necessary
16
+ * redirection functions or other settings.
17
+ * @returns {React.FunctionComponent<DotcmsPageProps>} A component that wraps the original component,
18
+ * adding experiment handling based on the specified configuration.
19
+ */
20
+ export declare const withExperiments: (WrappedComponent: React.ComponentType<DotcmsPageProps>, config: DotExperimentConfig) => (props: DotcmsPageProps) => import("react/jsx-runtime").JSX.Element;
@@ -1,3 +1,4 @@
1
+ /// <reference types="react" />
1
2
  import { DotExperiments } from '../dot-experiments';
2
3
  /**
3
4
  * `DotExperimentsContext` is a React context that is designed to provide an instance of
@@ -0,0 +1,21 @@
1
+ /**
2
+ * A React Hook that determines whether to wait for the correct variant in an A/B testing scenario.
3
+ * This is used to avoid flickering - showing the original content before redirecting to the assigned variant.
4
+ *
5
+ * The hook uses the running experiment id and viewAs (containing variantId) from the provided data.
6
+ * It then works with the DotExperimentsContext to synchronize between the assigned variant and the one requested.
7
+ * If the hook is executed inside an editor or if no running experiment id is provided, it immediately signals not to wait for the variant.
8
+ * Similarly, if the assigned variant matches the requested one, it signals not to wait for the variant.
9
+ * By default, the hook signals to wait for the variant.
10
+ *
11
+ * @param {Object} data - An object containing the runningExperimentId and viewAs (containing variantId).
12
+ * @returns {Object} An object with a function `shouldWaitForVariant` that, when called, returns `true` if it should wait for the correct variant, `false` otherwise.
13
+ */
14
+ export declare const useExperimentVariant: (data: {
15
+ runningExperimentId?: string;
16
+ viewAs: {
17
+ variantId: string;
18
+ };
19
+ }) => {
20
+ shouldWaitForVariant: boolean;
21
+ };