--- url: /reference/items/Fetch/examples.md --- # Examples ## Simple Intercepting clicks on links, displaying a loader and updating the targets' content. :::code-group ```ts import { registerComponent } from '@studiometa/js-toolkit'; import { Action, Fetch, Transition } from '@studiometa/ui'; registerComponent(Action); registerComponent(Fetch); registerComponent(Transition); ``` ::: ## Fetch from any element `Fetch` normally reads its URL from an `` or `
`, but the [`src` option](./js-api.md#src) lets it be driven from **any** element and triggered programmatically. In the following example the panel is a `
`: it combines `Fetch` with the [`InViewOnce`](../InViewOnce/index.md) and [`Action`](../Action/index.md) components so that its content is lazy-loaded the first time it scrolls into view, with a bare [`Fetch.fetch()`](./js-api.md#fetch-url-url-string-requestinit-requestinit) call that resolves the `src` URL on its own. :::code-group ```ts import { registerComponent } from '@studiometa/js-toolkit'; import { Action, Fetch, InViewOnce } from '@studiometa/ui'; registerComponent(Action); registerComponent(Fetch); registerComponent(InViewOnce); ``` ::: ## Form In the following example, we intercept a form submission, display a loader and use a custom view transition to animate only the updated content once the request is finished. :::code-group ```css html.dark { background-color: #222; color: #eee; } body { padding: 1rem; } @keyframes loader { 0% { transform: rotate(0) } 100% { transform: rotate(720deg) } } .animate-loader { animation: loader 1s ease-in-out infinite; } #target strong { view-transition-name: target; } ::view-transition-old(target) { animation: transition-out 0.3s ease-in-out; } ::view-transition-new(target) { animation: transition-in 0.3s ease-in-out; } @keyframes transition-out { to { opacity: 0; transform: translateY(-100%); } } @keyframes transition-in { from { opacity: 0; transform: translateY(100%); } } ``` ::: ## Modes Modes are configured with the [`data-option-mode` attribute](./js-api.md#mode). :::code-group ```ts import { registerComponent } from '@studiometa/js-toolkit'; import { Fetch } from '@studiometa/ui'; registerComponent(Fetch); ``` ::: ## Add to cart / Quickbuy :::code-group ```ts import { registerComponent } from '@studiometa/js-toolkit'; import { Action, Dialog, Fetch, Figure, Transition } from '@studiometa/ui'; registerComponent(Action); registerComponent(Dialog); registerComponent(Fetch); registerComponent(Figure); registerComponent(Transition); ``` ::: ## Error handling :::code-group ```ts import { registerComponent } from '@studiometa/js-toolkit'; import { Action, DataBind, Fetch, Transition } from '@studiometa/ui'; registerComponent(Action); registerComponent(DataBind); registerComponent(Fetch); registerComponent(Transition); ``` ::: ## Cancelling a request Use the [`abort` method](./js-api.md#abort-reason-any) to cancel a request. In the following example, we use the [`Action` component](../Action/index.md) to cancel any pending request from any mounted `Fetch` component. :::code-group ```ts import { registerComponent } from '@studiometa/js-toolkit'; import { Action, DataBind, Fetch, Transition } from '@studiometa/ui'; registerComponent(Action); registerComponent(DataBind); registerComponent(Fetch); registerComponent(Transition); ``` ::: ::: tip In this example, we could use a more specific version of the [`data-option-target` attribute](../Action/js-api.md#target) of the `Action` component to target a single `Fetch` instance. ::: ## Shopify Section Rendering API Shopify's [Section Rendering API](https://shopify.dev/docs/api/ajax/section-rendering) returns the rendered HTML of specific theme sections as a JSON object: each key is a requested section ID and each value is that section's HTML — or `null` when the section fails to render. Because every section is wrapped in a `
` element both on the page and inside the API response, the `Fetch` component can consume it **without any extra code**: parse the JSON with the [`response` option](./js-api.md#response) and let the default [`[id]` selector](./js-api.md#selector) swap each section in place. ::: tip The [`FetchShopifySection`](../FetchShopifySection/index.md) component wraps this pattern: it declares the sections through a `sections` option (keeping them out of the `href` for a working no-JS fallback) and ships the JSON extraction below as its default, so you don't repeat it on every element. ::: Request the sections to update by adding the comma-separated `sections` parameter (up to five) to the URL: :::code-group ```html [collection.liquid] Sort by price {% comment %} These wrappers, rendered by Shopify, are what Fetch swaps by id. {% endcomment %}
…
…
``` ```json [GET ?sections=… response] { "main-collection-product-grid": "
…
", "collection-results-count": "
…
" } ``` ::: * `filter(Boolean)` drops any section returned as `null` (for example a section absent from the published theme) so the parser only receives valid HTML. * The JSON keys are ignored: `Fetch` matches each section by the `id` of its `shopify-section-*` wrapper, so there is never a key/id mismatch. * Keep the default `replace` [`mode`](./js-api.md#mode) (or use `morph`) so each section is swapped in place — `append` and `prepend` insert the new markup *inside* the existing `shopify-section-{id}` wrapper and would duplicate its content. This works together with the [`history`](./js-api.md#history) and [`viewTransition`](./js-api.md#viewtransition) options. ::: tip Use a `` instead of a link when the parameters come from user input (facet filters, a sort ``. :::