JSPM

@localess/react

0.9.2-dev.20260307154946
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 720
  • Score
    100M100P100Q104095F
  • License MIT

ReactJS JavaScript/TypeScript SDK for Localess's API.

Package Exports

  • @localess/react

Readme



logo


Localess React

This client SDK is designed to work with the Localess API. It provides a simple way to interact with the Localess API from your React application.

Installation

NPM

npm install @localess/react

Yarn

yarn add @localess/react

Usage

Initialize and Visual Editor

import { localessInit } from "@localess/react";
import { Page, Header, Teaser, Footer } from "@/components"

localessInit({
  origin: "https://my-localess.web.app",
  spaceId: "I1LoVe2LocaLess4Rever",
  token: "Baz00KaT0KeN8S3CureLL",
  enableSync: true, //Enable Visual Editor Sync Script,
  components: {
    'page': Page,
    'header': Header,
    'teaser': Teaser,
    'footer': Footer,
  },
})

React Component

Example of Header Component with Menu Items

import { llEditable, LocalessComponent } from "@localess/react";

const Header = ({data}) => {
  return (
    <nav {...llEditable(data)}>
      {data.items.map(item => <LocalessComponent key={item._id} data={item} />)}
    </nav>
  )
}

Listen for Visual Editor Events

Your application can subscribe to the Localess Visual Editor Events. Example from NextJS 15

import { llEditable, LocalessComponent, getLocalessClient } from "@localess/react";

export default async function Page({searchParams}: {
  searchParams: Promise<{ [key: string]: string | string[] | undefined }>
}) {
  const {locale} = await searchParams
  const {data} = await fetchData(locale?.toString());
  const [ pageData, setPageData ] = useState(data);
  
  
  if (window.localess) {
    window.localess.on(['input', 'change'], (event) => {
      if (event.type === 'input' || event.type === 'change') {
        setPageData(event.data);
      }
    });
  }
  return (
    <main {...llEditable(data)}>
      {data.body.map(item => <LocalessComponent key={item._id} data={item} />)}
    </main>
  )
}

async function fetchData(locale?: string): Promise<Content<Page>> {
  const client = getLocalessClient(); // Get LocalessClient Initlialized before
  return client.getContentBySlug<Page>('home', {locale: locale ? locale : undefined});
}