npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

crs-arch

v0.0.9

Published

Components Router State

Downloads

5

Readme

CRS-Arch Library

CRS-Arch is a lightweight library for building reactive web applications using a component-based architecture. It provides utilities for creating state management, reactive components, and initializing your application. Additionally, it includes a simple yet powerful router for handling navigation within your application.

Features

  • State Management: Easily create and manage application state.
  • Reactive Components: Build components that react to state changes.
  • Component Composition: Compose complex UIs from simple components.
  • Event Handling: Attach event listeners to components for interactivity.
  • Routing: Manage navigation with a straightforward router.

Installation

To install CRS-Arch, use npm or yarn:

npm install crs-arch

Usage

Below is a simple example demonstrating how to use CRS-Arch to build a reactive application with routing.

Example

State Management and Components

import { createComponent, createState, initApp, observe } from 'crs-arch'
import { ComponentOptions } from 'crs-arch/types'

interface initialState {
  count: number
}

const store = createState<initialState>({
  count: 0
})

const LinkImage = ({ href, className, alt, srcLogic }: any): HTMLElement => {
  const props: ComponentOptions<'img'> = {
    tag: 'img',
    class: className,
    alt,
    src: srcLogic(store.state.count)
  }
  const render = (state: initialState): { src: string } => ({ src: srcLogic(state.count) })

  return createComponent({
    tag: 'a',
    href,
    children: [observe({
      store,
      props,
      render
    })]
  })
}

const CounterButton = (): HTMLElement => {
  const props: ComponentOptions<'button'> = {
    tag: 'button',
    id: 'counter',
    type: 'button',
    content: `count is ${store.state.count}`,
    events: {
      click: () => store.set({ count: store.state.count + 1 })
    }
  }

  const render = (state: initialState): { content: string } => ({ content: `count is ${state.count}` })

  return observe({
    store,
    props,
    render
  })
}

const App = (): HTMLElement =>
  createComponent({
    children: [
      LinkImage({
        href: 'https://developer.mozilla.org/en-US/docs/Web/JavaScript',
        className: 'logo',
        alt: 'JavaScript logo',
        srcLogic: (count: number) => (count % 2 === 0 ? javascriptLogo : viteLogo)
      }),
      LinkImage({
        href: 'https://vitejs.dev',
        className: 'logo vanilla',
        alt: 'Vite logo',
        srcLogic: (count: number) => (count % 2 !== 0 ? javascriptLogo : viteLogo)
      }),
      createComponent({
        tag: 'h1',
        content: 'Hello Vite!'
      }),
      createComponent({
        class: 'card',
        children: [CounterButton]
      }),
      createComponent({
        tag: 'p',
        class: 'read-the-docs',
        content: 'Click on the Vite logo to learn more'
      })
    ]
  })

initApp('app', [App])

Routing

// routes.ts
import { createRouter } from 'crs-arch'
import { About } from './pages/about'
import { Home } from './pages/home'
import { Main } from './pages/main/view/main'

export const { add, layout, navigate, start } = createRouter()

add('/about', About)
add('/main', Main)
add('/', Home)

// layout.ts
import { createComponent } from 'crs-arch'
import { layout } from './routes'
import './style.css'

export function RootLayout (): HTMLElement {
  return layout(createComponent({
    class: 'layout'
  }))
}

// navigation.ts
import { createComponent } from 'crs-arch'
import { Button } from '../button/button'
import { navigate } from './routes'

export function Navigation (): HTMLElement {
  return createComponent({
    tag: 'header',
    class: 'navigation',
    children: [
      Button({
        title: 'About',
        onClick: () => navigate('/about')
      }),
      Button({
        title: 'Main',
        onClick: () => navigate('/main')
      }),
      Button({
        title: 'Home',
        onClick: () => navigate('/')
      })
    ]
  })
}

// main.ts
import { initApp } from 'crs-arch'
import { Navigation } from './components/navigation/navigation'
import { Title } from './components/title/title'
import { start } from './routes'
import { RootLayout } from './layout/root-layout'

initApp('app', [
  Navigation,
  Title,
  RootLayout
])

start()

Explanation

  1. State Management: The createState function initializes the application state.
  2. Reactive Components: The observe function makes components reactive to state changes.
  3. Component Composition: The createComponent function is used to compose components.
  4. Event Handling: Event listeners are attached to components using the events property.
  5. Routing: The createRouter function sets up the router, and add adds routes. The navigate function handles navigation, and layout defines the layout structure.

API Reference

createState<T>(initialState: T): { state: T, set: (newState: Partial<T>) => void }

Creates a state management object with the initial state and a setter function.

createComponent(options: ComponentOptions<T>): HTMLElement

Creates a new component with the given options.

observe({ store, props, render }: { store: { state: T, set: (newState: Partial<T>) => void }, props: ComponentOptions<T>, render: (state: T) => Partial<ComponentOptions<T>> }): HTMLElement

Makes a component reactive by observing the state changes and re-rendering the component accordingly.

initApp(rootId: string, components: Array<() => HTMLElement>): void

Initializes the application by rendering the provided components into the specified root element.

createRouter(): { add: (path: string, component: () => HTMLElement) => void, layout: (layout: HTMLElement) => HTMLElement, navigate: (path: string) => void, start: () => void }

Creates a router instance with methods to add routes, define layout, navigate, and start the router.

License

This project is licensed under the MIT License. See the LICENSE file for details.