0.54.0 • Published 11 days ago

@searchspring/snap-preact v0.54.0

Weekly downloads
-
License
MIT
Repository
github
Last release
11 days ago

Snap Preact

Snap Preact is an abstraction layer that provides a config based interface for creating a Searchspring integration quickly.

Installation

To install the snap-preact package and it's dependencies:

npm install --save @searchspring/snap-preact

Instantiation

import { Snap } from '@searchspring/snap-preact';

const snap = new Snap(config);

Configuration

A configuration object provided to Snap will determine the services that will be created.

Full example:

const config = {
	context: globalContext,
	url: {
		parameters: {
			core: {
				query: { name: 'search' },
			},
		},
	},
	client: {
		globals: {
			siteId: 'xxxxxx',
		},
	},
	controllers: {
		search: [
			{
				config: {
					id: 'search',
					settings: {
						redirects: {
							merchandising: false,
						},
					},
				},
				targets: [
					{
						selector: '#searchspring-content',
						component: () => Content,
						hideTarget: true,
					},
					{
						selector: '#searchspring-sidebar',
						component: () => Sidebar,
						hideTarget: true,
					},
				],
			},
		],
		autocomplete: [
			{
				config: {
					id: 'autocomplete',
					selector: 'input.searchspring-ac',
					settings: {
						trending: {
							limit: 5,
						},
					},
				},
				targets: [
					{
						selector: 'input.searchspring-ac',
						component: () => Autocomplete,
						hideTarget: true,
					},
				],
			},
		],
	},
};

config.context - optional Context object to be used to set the global context. If no context is provided, a default context taken from the integration script (shopper variable) will be used, otherwise the provided config.context is merged with the script context. This context becomes the globalContext that is passed to all controllers that are created.

config.client

A single client instance will be created and shared across all services using the provided config.

See @searchspring/snap-client documentation for full client config options.

const config = {
	client: {
		globals: {
			siteId: 'xxxxxx'
		}
	}
}

config.instantiators

The instantiators object must be defined if any Recommendation controllers have also been defined via config.controllers.recommendation

const config = {
	instantiators: {
		recommendation: {
			context: recommendationContext,
			components: {
				Standard: () => Standard
			},
			config: {
				batched: true
			},
			selector: '',
			services: {}
		}
	},
	controllers: {
		recommendation: []
	}
}

recommendation.components - required mapping of recommendation components.

recommendation.context - optional Context object to be used to set controller specific context. Defaults to the global context if no context prop is provided, or if one is provided, it is merged into the global context.

recommendation.config.branch - optional current git branch name - defaults to 'production'

recommendation.config.batched - optional boolean (default: true) to batch multiple recommendations into a single network request

recommendation.selector - optional selector to target recommendation instances if using a non-standard installation. Selector provided will replace the default selector: script[type="searchspring/recommend"], script[type="searchspring/personalized-recommendations"]

recommendation.services - optional object of ControllerServices

config.url

The url object contains the config provided to each UrlTranslator created by Snap Preact.

const config = {
	url: {
		parameters: {
			core: {
				query: { name: 'search' },
				page: { name: 'p' }
			},
		},
	},
}

config.controllers

The controllers object contains a list of controllers to create for each controller type.

Available controllers:

const config = {
	controllers: {
		search: [],
		autocomplete: [],
		finder: [],
		recommendation: [],
	}
}

Each array entry contains an object with the following properties:

config - required controller config for the corresponding controller. See Controller specific documentation for all available configuration options.

targets - optional array of Target objects. Targets that have been found will have the corresponding controller provided to the target component controller prop and the controller's search method invoked.

type ExtendedTarget = {
	selector: string;
	inject?: {
		action: 'before' | 'after' | 'append' | 'prepend' | 'replace';
		element: Element | ((target: Target, element: Element) => Element);
	};
	hideTarget?: boolean;
	autoRetarget?: boolean;
	clickRetarget?: boolean | string;
	emptyTarget?: boolean;
	name?: string;
	component?: () => Promise<RootComponent> | RootComponent;
	props?: unknown;  // additional props to pass to the component
	onTarget?: OnTarget;  // additional scripts to execute when target is found
	prefetch?: boolean;  // run controller search before finding targets
}

services - optional object of ControllerServices to be used for this controller in place of the default services

url - optional UrlTranslator config object to be used with the UrlManager for this controller

context - optional Context object to be used to set controller specific context. Defaults to the global context if no context prop is provided, or if one is provided, it is merged into the global context.

An example creating a SearchController:

const config = {
	controllers: {
		search: [
			{
				context: searchContext,
				config: {
					id: 'search',
				},
				targets: [
					{
						selector: '#searchspring-content',
						component: () => Content,
						hideTarget: true,
					},
					{
						selector: '#searchspring-sidebar',
						component: () => Sidebar,
						hideTarget: true,
					},
				],
				services: {}
			}
		]
	}
}

The controller config id will be the name of the controller that you will then interface from the return of creating the new Snap() instance via the controllers object.

For example, if using the config example above:

const snap = new Snap(config);
const { search } = snap.controllers;

properties

After instantiating an instance of Snap, the following properties can be accessed.

eventManager

A reference to the shared @searchspring/snap-event-manager instance.

config

A reference to the config that was provided.

logger

A reference to the shared @searchspring/snap-logger instance used by each controller.

client

A reference to the shared @searchspring/snap-client instance used by each controller.

tracker

A reference to the shared @searchspring/snap-tracker instance used by each controller.

controllers

An object containing all controllers that have been created.

recommendations

A reference to RecommendationInstantiator instance if creating recommendation instances.

polyfills

Snap Preact provides various polyfills to ensure legacy browser support.

import { polyfills } from '@searchspring/snap-preact';

polyfills.then(() => {
	import('./index');
})
0.54.0

11 days ago

0.53.4

25 days ago

0.53.3

1 month ago

0.53.2

1 month ago

0.53.0

2 months ago

0.53.1

1 month ago

0.52.2

2 months ago

0.52.1

2 months ago

0.52.0

2 months ago

0.51.2

3 months ago

0.51.1

3 months ago

0.51.0

4 months ago

0.50.0

4 months ago

0.48.0

7 months ago

0.46.0

10 months ago

0.49.1

7 months ago

0.49.0

7 months ago

0.47.0

8 months ago

0.44.2

1 year ago

0.44.3

1 year ago

0.44.0

1 year ago

0.44.1

1 year ago

0.45.1

11 months ago

0.45.0

12 months ago

0.43.0

1 year ago

0.42.2

1 year ago

0.42.3

1 year ago

0.42.0

1 year ago

0.42.1

1 year ago

0.43.1

1 year ago

0.41.1

1 year ago

0.41.2

1 year ago

0.41.0

1 year ago

0.40.0

1 year ago

0.39.3

1 year ago

0.39.1

1 year ago

0.39.0

1 year ago

0.39.2

1 year ago

0.38.1

1 year ago

0.38.0

2 years ago

0.37.1

2 years ago

0.37.0

2 years ago

0.36.0

2 years ago

0.35.0

2 years ago

0.34.6

2 years ago

0.34.5

2 years ago

0.34.4

2 years ago

0.34.3

2 years ago

0.34.2

2 years ago

0.34.1

2 years ago

0.34.0

2 years ago

0.33.0

2 years ago

0.34.7

2 years ago

0.32.0

2 years ago

0.30.2

2 years ago

0.31.0

2 years ago

0.30.1

2 years ago

0.30.0

2 years ago

0.29.0

2 years ago

0.28.0

2 years ago

0.27.2

2 years ago

0.27.1

2 years ago

0.27.0

2 years ago

0.27.8

2 years ago

0.27.7

2 years ago

0.27.6

2 years ago

0.27.5

2 years ago

0.27.4

2 years ago

0.27.3

2 years ago

0.26.1

2 years ago

0.26.0

2 years ago

0.25.1

2 years ago

0.25.0

2 years ago

0.24.0

2 years ago

0.23.1

2 years ago

0.23.0

2 years ago

0.22.0

2 years ago

0.21.1

2 years ago

0.21.0

2 years ago

0.20.5

2 years ago

0.20.4

2 years ago

0.20.3

2 years ago

0.20.2

2 years ago

0.20.1

2 years ago

0.20.0

2 years ago