Skip to content

Latest commit

 

History

History

fetch

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 

README.md

@tailwind-stack/fetch

Like TailwindCSS, but for fetch(). className your way to API data!

Installation

npm install @tailwind-stack/fetch

Usage

import { Fetch } from "@tailwind-stack/fetch";

<Fetch className="fetch-[https://api.example.com/users/1]-select-name" />;

Syntax

fetch-[url]-select-path.to.data
Part Description
fetch-[url] The URL to fetch
-select-path Dot notation selector
as prop Render format

Components

Component Type Description
Fetch Server (RSC) Fetches on server, zero client JS
FetchClient Client Fetches in browser with loading state

Examples

// Basic fetch with selector
<Fetch className="fetch-[https://jsonplaceholder.typicode.com/posts/1]-select-title" />

// Table render
<Fetch className="fetch-[https://api.example.com/users]" as="table" />

// JSON display
<Fetch className="fetch-[https://api.example.com/data]" as="json" />

// Array index selector
<Fetch className="fetch-[.../comments]-select-0.body" />

// Client component with loader
<FetchClient className="fetch-[url]" loader={<Spinner />} />

URL Aliases

import { setConfig } from '@tailwind-stack/fetch'

setConfig({
  aliases: {
    jp: 'https://jsonplaceholder.typicode.com',
    api: 'https://api.example.com',
  },
})

// Use with $ prefix
<Fetch className="fetch-[$jp/users/1]" />

Token Syntax (Children)

Pass data to children with $$response:

<Fetch className="fetch-[.../users/1]">
  <QR className="qr-encode-[$$response.website]" />
</Fetch>

<Fetch className="fetch-[.../users/1]">
  <span className="font-bold $$response.name" />
</Fetch>
Token Result
$$response Full response
$$response.name Nested property
$$response.users[0].id Array access

Render Formats

Format Output
span Default text
div Block element
ul / ol List
table HTML table
json / code JSON block

Props

Prop Type Default Description
className string - Fetch directive
as RenderAs "span" Render format
style CSSProperties - Inline styles
loader ReactNode - Loading state (FetchClient)
children ReactNode - With $$response tokens

License

MIT