@findlane/browser 0.27.0 → 0.27.1

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/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  export { imageWidth, imageWidths } from "./images.js";
2
- export { placeholderUrl } from "./placeholder.js";
2
+ export { placeholderCss, placeholderUrl, revealCss } from "./placeholder.js";
3
3
  export type SearchRequest = {
4
4
  q?: string;
5
5
  /** A sort ID from the index settings, "relevance", or "distance" (with `near`). */
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { imageHostsHeader, imageHostsOf, optimizedImageUrl } from "./images.js";
2
2
  export { imageWidth, imageWidths } from "./images.js";
3
- export { placeholderUrl } from "./placeholder.js";
3
+ export { placeholderCss, placeholderUrl, revealCss } from "./placeholder.js";
4
4
  /** A query suggestion's text in parts: what was typed (`typed: true`) and the rest. */
5
5
  export function suggestionParts(suggestion) {
6
6
  const [start, end] = suggestion.typed ?? [0, 0];
@@ -1,3 +1,17 @@
1
1
  /** A hit's `placeholder` (a ThumbHash in base64) drawn as a PNG data URL, about 32 pixels on its longer side, to show
2
2
  * stretched to the image's box while the image loads; null for one that can't be read. */
3
3
  export declare function placeholderUrl(placeholder: string | null | undefined): string | null;
4
+ /** How long the image takes to sharpen in, in milliseconds. */
5
+ export declare const revealMs = 350;
6
+ /** The CSS while the image loads, as a style attribute's text: for pages that draw their own results, on the img
7
+ * until its load event, then `revealCss` for revealMs, then no style. */
8
+ export declare const placeholderCss: (url: string) => string;
9
+ /** The same, as React's style object. */
10
+ export declare function placeholderStyle(url: string): Record<string, string>;
11
+ /** The CSS that sharpens the image in, once it has loaded. */
12
+ export declare const revealCss = "filter:blur(0);opacity:1;transition:filter 350ms ease-out, opacity 350ms ease-out";
13
+ export declare const revealStyle: {
14
+ filter: string;
15
+ opacity: number;
16
+ transition: string;
17
+ };
@@ -151,3 +151,25 @@ export function placeholderUrl(placeholder) {
151
151
  drawn.set(placeholder, url);
152
152
  return url;
153
153
  }
154
+ // Blur-up: how a placeholder shows while its image loads, the same in React's Image and the search widget. It sits in
155
+ // the image's content box (as object-fit: contain places the image), blurred and faded with feathered edges, so it
156
+ // hints at the product's colour and size without hard edges; when the image arrives it starts blurred and sharpens
157
+ // over revealMs, then every inline style is taken off, leaving the page's own CSS as it was.
158
+ const mask = "radial-gradient(closest-side, #000 45%, transparent 90%)";
159
+ const waitingRules = (url) => [
160
+ ["background-image", `url("${url}")`], ["background-size", "contain"], ["background-position", "center"],
161
+ ["background-repeat", "no-repeat"], ["background-origin", "content-box"], ["background-clip", "content-box"],
162
+ ["filter", "blur(14px)"], ["opacity", "0.55"], ["-webkit-mask-image", mask], ["mask-image", mask],
163
+ ];
164
+ /** How long the image takes to sharpen in, in milliseconds. */
165
+ export const revealMs = 350;
166
+ /** The CSS while the image loads, as a style attribute's text: for pages that draw their own results, on the img
167
+ * until its load event, then `revealCss` for revealMs, then no style. */
168
+ export const placeholderCss = (url) => waitingRules(url).map(([name, value]) => `${name}:${value}`).join(";");
169
+ /** The same, as React's style object. */
170
+ export function placeholderStyle(url) {
171
+ return Object.fromEntries(waitingRules(url).map(([name, value]) => [name.replace(/^-webkit-/, "Webkit-").replace(/-([a-z])/g, (_, letter) => letter.toUpperCase()), value]));
172
+ }
173
+ /** The CSS that sharpens the image in, once it has loaded. */
174
+ export const revealCss = `filter:blur(0);opacity:1;transition:filter ${revealMs}ms ease-out, opacity ${revealMs}ms ease-out`;
175
+ export const revealStyle = { filter: "blur(0)", opacity: 1, transition: `filter ${revealMs}ms ease-out, opacity ${revealMs}ms ease-out` };
@@ -14,12 +14,12 @@ export type ImageProps = Omit<ImgHTMLAttributes<HTMLImageElement>, "src" | "srcS
14
14
  /** The client whose index the image is from: by default the surrounding SearchProvider's, SearchBox's or
15
15
  * Recommendations', else the FindlaneProvider's. */
16
16
  client?: SearchBrowserClient;
17
- /** The hit's `placeholder`: a soft preview of the image, shown behind it until it loads, fitted as
18
- * `object-fit: contain` fits the image (set `backgroundSize` in `style` for another fit). */
17
+ /** The hit's `placeholder`: a soft, blurred hint of the image's colour and size, shown in its place until it
18
+ * loads, when the image sharpens in. */
19
19
  placeholder?: string | null;
20
20
  };
21
21
  /** A record's image (0.27.0): resized to its width (and twice that for sharp screens) and sent as WebP when the
22
22
  * index has optimized images on and the image is on a host it allows (SearchBrowserClient.imageUrl); otherwise
23
- * the image as it is, with its placeholder behind it until it loads. Lazy by default; if the resized image fails
24
- * to load, the original is loaded instead. */
23
+ * the image as it is. With a placeholder (0.27.1), a blurred hint of the image shows until it loads, then the image
24
+ * sharpens in. Lazy by default; if the resized image fails to load, the original is loaded instead. */
25
25
  export declare function Image({ src, width, client: given, placeholder, alt, loading, decoding, onError, onLoad, style, ...rest }: ImageProps): import("react").JSX.Element | null;
@@ -1,7 +1,7 @@
1
1
  "use client";
2
2
  import { jsx as _jsx } from "react/jsx-runtime";
3
- import { createContext, useContext, useState } from "react";
4
- import { placeholderUrl } from "../placeholder.js";
3
+ import { createContext, useContext, useEffect, useState } from "react";
4
+ import { placeholderStyle, placeholderUrl, revealMs, revealStyle } from "../placeholder.js";
5
5
  import { useProvidedClient } from "./client.js";
6
6
  const ImageClientContext = createContext(null);
7
7
  /** The client the Images inside show their index's images for: SearchProvider, SearchBox and Recommendations
@@ -11,31 +11,42 @@ export function ImageClient({ client, children }) {
11
11
  }
12
12
  /** A record's image (0.27.0): resized to its width (and twice that for sharp screens) and sent as WebP when the
13
13
  * index has optimized images on and the image is on a host it allows (SearchBrowserClient.imageUrl); otherwise
14
- * the image as it is, with its placeholder behind it until it loads. Lazy by default; if the resized image fails
15
- * to load, the original is loaded instead. */
14
+ * the image as it is. With a placeholder (0.27.1), a blurred hint of the image shows until it loads, then the image
15
+ * sharpens in. Lazy by default; if the resized image fails to load, the original is loaded instead. */
16
16
  export function Image({ src, width, client: given, placeholder, alt = "", loading = "lazy", decoding = "async", onError, onLoad, style, ...rest }) {
17
17
  const shown = useContext(ImageClientContext);
18
18
  const provided = useProvidedClient();
19
19
  const client = given ?? shown ?? provided;
20
20
  // The address whose resized image didn't load: it shows as it is from then on.
21
21
  const [failed, setFailed] = useState(null);
22
- // The address that has loaded, whose placeholder is gone.
22
+ // The address that has loaded, and the one whose sharpening has finished; then no style of ours is left.
23
23
  const [loaded, setLoaded] = useState(null);
24
+ const [settled, setSettled] = useState(null);
25
+ const one = src ? (client && failed !== src ? client.imageUrl(src, width) : src) : "";
26
+ const preview = placeholderUrl(placeholder);
27
+ const revealing = preview !== null && loaded === one && settled !== one;
28
+ useEffect(() => {
29
+ if (!revealing)
30
+ return;
31
+ const timer = setTimeout(() => setSettled(one), revealMs + 50);
32
+ return () => clearTimeout(timer);
33
+ }, [revealing, one]);
24
34
  if (!src)
25
35
  return null;
26
- const one = client && failed !== src ? client.imageUrl(src, width) : src;
27
36
  const optimized = one !== src;
28
37
  const two = optimized ? client.imageUrl(src, width * 2) : one;
29
- const preview = loaded === one ? null : placeholderUrl(placeholder);
30
- const behind = preview
31
- ? { backgroundImage: `url("${preview}")`, backgroundSize: "contain", backgroundPosition: "center", backgroundRepeat: "no-repeat" }
32
- : undefined;
33
- return (_jsx("img", { ...rest, src: one, srcSet: two !== one ? `${one} 1x, ${two} 2x` : undefined, width: width, alt: alt, loading: loading, decoding: decoding, style: behind ? { ...behind, ...style } : style,
38
+ const waiting = preview !== null && loaded !== one;
39
+ const ours = waiting ? placeholderStyle(preview) : revealing ? revealStyle : undefined;
40
+ return (_jsx("img", { ...rest, src: one, srcSet: two !== one ? `${one} 1x, ${two} 2x` : undefined, width: width, alt: alt, loading: loading, decoding: decoding, style: ours ? { ...ours, ...style } : style,
34
41
  // Searches are sent without credentials, so an image sent without them comes over the same connection.
35
42
  crossOrigin: optimized ? "anonymous" : undefined,
36
- // An image the browser had already shows at once, so its placeholder goes as soon as it's drawn.
37
- ref: preview ? (node) => { if (node?.complete && node.naturalWidth)
38
- setLoaded(one); } : undefined, onLoad: (event) => {
43
+ // An image the browser had already shows at once, without its placeholder.
44
+ ref: waiting ? (node) => {
45
+ if (node?.complete && node.naturalWidth) {
46
+ setLoaded(one);
47
+ setSettled(one);
48
+ }
49
+ } : undefined, onLoad: (event) => {
39
50
  setLoaded(one);
40
51
  onLoad?.(event);
41
52
  }, onError: (event) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@findlane/browser",
3
- "version": "0.27.0",
3
+ "version": "0.27.1",
4
4
  "description": "Findlane browser SDK: fast, typo-tolerant public search from the browser with an index's public ID, plus React hooks and components.",
5
5
  "keywords": ["findlane", "search", "full-text search", "typo tolerance", "instant search", "ecommerce search", "browser", "react", "autocomplete"],
6
6
  "license": "MIT",