Skip to content

Latest commit

History

386 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

@uniweb/core

Core classes for the Uniweb Component Web Platform.

Overview

This package provides the foundational classes that power Uniweb sites. It's a pure JavaScript library with no React dependencies, designed to be shared between the runtime and foundations.

Installation

npm install @uniweb/core

Usage

import{createUniweb,getUniweb}from'@uniweb/core'// Create the singleton instance (typically done by @uniweb/runtime)constuniweb=createUniweb(siteConfig)// Access the singleton from anywhereconstuniweb=getUniweb()// Work with the active websiteconstwebsite=uniweb.activeWebsiteconstpage=website.getPage('/about')constlanguage=website.getLanguage()

API

Factory Functions

FunctionDescription
createUniweb(config)Create and register the global Uniweb instance
getUniweb()Get the current Uniweb instance

Classes

Uniweb

The main runtime instance, available as globalThis.uniweb.

uniweb.getComponent(name)// Get component from foundationuniweb.listComponents()// List available componentsuniweb.activeWebsite// Current website instanceuniweb.setFoundation(module)// Set the foundation module

Website

Manages pages, theme, and localization.

// Page navigationwebsite.getPage(route)// Get page by routewebsite.setActivePage(route)// Navigate to pagewebsite.activePage// Current active pagewebsite.pages// All pageswebsite.pageRoutes// Array of route strings// Page Hierarchy API (for navbars, footers, sitemaps)website.getPageHierarchy(options)// Get pages for navigationwebsite.getHeaderPages()// Convenience: pages for header navwebsite.getFooterPages()// Convenience: pages for footer navwebsite.getAllPages()// Get flat list of all pages// Locale APIwebsite.getLocales()// Get all locales: [{code, label, isDefault}]website.getActiveLocale()// Get current locale codewebsite.getDefaultLocale()// Get default locale codewebsite.hasMultipleLocales()// Check if site has multiple localeswebsite.getLocaleUrl(code,route)// Build URL for a localewebsite.setActiveLocale(code)// Set active localewebsite.getLocale(code)// Get locale info by code// Content localization (for multilingual values)website.localize(value)// Localize {en: "Hello", es: "Hola"} to active langwebsite.makeHref(href)// Transform href for routing// Deprecated (use Locale API instead)website.getLanguage()// Use getActiveLocale()website.getLanguages()// Use getLocales()

Page Hierarchy API

The getPageHierarchy() method returns pages filtered and formatted for navigation:

// Get pages for header navigationconstheaderPages=website.getPageHierarchy({for: 'header'})// Returns: [{ id, route, title, label, description, order, hasContent, children }]// Get flat list of all pages (for sitemaps)constallPages=website.getPageHierarchy({nested: false,includeHidden: true})// Custom filtering and sortingconsttopLevel=website.getPageHierarchy({filter: (page)=>page.order<10,sort: (a,b)=>a.title.localeCompare(b.title)})

Options:

  • nested (default: true) - Return with nested children or flat list
  • for - Filter for 'header', 'footer', or undefined (all)
  • includeHidden (default: false) - Include hidden pages
  • filter - Custom filter function: (page) => boolean
  • sort - Custom sort function: (a, b) => number

Page

Represents a page with its sections.

// Basic propertiespage.route// Page route pathpage.title// Page titlepage.description// Page descriptionpage.label// Short navigation label (or null)page.order// Sort orderpage.children// Child pages (for nested hierarchy)page.website// Back-reference to parent Website// Navigation visibilitypage.hidden// Hidden from all navigationpage.hideIn// Nav areas this page is hidden from (e.g. ['header'])page.showInNav(area)// Should appear in a given named nav area?page.isHidden()// Check if hidden from navigationpage.showInHeader()// Should appear in header nav?page.showInFooter()// Should appear in footer nav?page.getLabel()// Get navigation label (falls back to title)// Layout options (per-page overrides)page.layout.header// Show header on this page?page.layout.footer// Show footer on this page?page.layout.leftPanel// Show left panel?page.layout.rightPanel// Show right panel?page.hasHeader()// Convenience: page.layout.headerpage.hasFooter()// Convenience: page.layout.footerpage.hasLeftPanel()// Convenience: page.layout.leftPanelpage.hasRightPanel()// Convenience: page.layout.rightPanel// Contentpage.getPageBlocks()// Get header + body + footer blockspage.getBodyBlocks()// Get just body blockspage.getHeader()// Get header blockpage.getFooter()// Get footer blockpage.hasChildren()// Has child pages?page.getHeadMeta()// Get SEO meta tags

Page Configuration (page.yml)

title: About Usdescription: Learn about our companylabel: About # Short nav label (optional)order: 2# Navigation visibilityhidden: true # Hide from all navigationhideIn: [header] # Hide from named nav areas only (header, footer, sidebar, …)# Layout overrides (default: all true)layout:
header: false # Don't show header on this pagefooter: false # Don't show footer on this pageleftPanel: false # Don't show left panelrightPanel: false # Don't show right panel# SEO (optional)seo:
noindex: falseimage: /about-og.png

Block

Represents a section/component on a page.

block.component// Component nameblock.getBlockContent()// Get parsed contentblock.getBlockProperties()// Get configuration propertiesblock.childBlockRenderer// Renderer for child blocks

Input

Handles form input fields with validation.

input.value// Current valueinput.validate()// Run validationinput.errors// Validation errors

Architecture

@uniweb/runtime (browser)
│
└── imports @uniweb/core
│
├── createUniweb() - creates singleton
├── Uniweb - main instance
├── Website - page/locale management
├── Page - page representation
├── Block - section representation
└── Input - form handling
@uniweb/kit (foundation components)
│
└── imports @uniweb/core
│
└── getUniweb() - access singleton

For Foundation Creators

Foundations typically don't import from @uniweb/core directly. Instead, use @uniweb/kit which provides React hooks and components that abstract the core:

// Prefer this (kit)import{useWebsite}from'@uniweb/kit'const{ localize, website }=useWebsite()// Instead of this (core)import{getUniweb}from'@uniweb/core'constwebsite=getUniweb().activeWebsite

Mark @uniweb/core as external in your foundation's Vite config:

// vite.config.jsexportdefault{build: {rollupOptions: {external: ['react','react-dom','react-router-dom','@uniweb/core']}}}

Related Packages

License

Apache 2.0

About

Plain JS classes for the Uniweb core.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages