# cssx-framework

> A CSS driven web framework that compiles semi-standard CSS into HTML content + styles at runtime

Latest version **0.1.13** (published 2024-12-17) · ISC license · 0 weekly downloads

## Install

```sh
npm install cssx-framework
pnpm add cssx-framework
yarn add cssx-framework
bun add cssx-framework
```

## Health

**Score 30/100 (F)** — status: maintenance-mode.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types; pre 1.0.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.13 |
| Published | 2024-12-17 |
| First published | 2024-04-10 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 4 |
| Unpacked size | 29.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Blake Watkin |
| Maintainers | blake9201 |
| Keywords | cssx, web framework, framework, css, javascript, css pre-processor |

## Links

- npm: https://www.npmjs.com/package/cssx-framework
- Repository: https://github.com/blake2573/cssx
- Homepage: https://github.com/blake2573/cssx#readme
- Issues: https://github.com/blake2573/cssx/issues
- npm.io page: https://npm.io/package/cssx-framework

## Dependencies (4)

- [html-tag-names](https://npm.io/package/html-tag-names.md) ^2.1.0
- [aria-attributes](https://npm.io/package/aria-attributes.md) ^2.0.1
- [html-event-attributes](https://npm.io/package/html-event-attributes.md) ^2.2.0
- [html-element-attributes](https://npm.io/package/html-element-attributes.md) ^3.4.0

## Alternatives

- [style-dictionary](https://npm.io/package/style-dictionary.md) — 2.0M weekly downloads
- [postcss-merge-idents](https://npm.io/package/postcss-merge-idents.md) — 1.7M weekly downloads
- [@fontsource/noto-sans](https://npm.io/package/@fontsource/noto-sans.md) — 93.0K weekly downloads
- [uglifycss](https://npm.io/package/uglifycss.md) — 71.6K weekly downloads
- [mat4-interpolate](https://npm.io/package/mat4-interpolate.md) — 23.3K weekly downloads

## Recent versions

- 0.1.13 (latest) — 2024-12-17
- 0.1.12 — 2024-09-28
- 0.1.11 — 2024-09-28
- 0.1.10 — 2024-09-28
- 0.1.9 — 2024-09-21
- 0.1.8 — 2024-09-19
- 0.1.7 — 2024-09-13
- 0.1.6 — 2024-09-13
- 0.1.5 — 2024-09-13
- 0.1.4 — 2024-09-12
- 0.1.3 — 2024-09-12
- 0.1.2 — 2024-09-09
- 0.1.1 — 2024-09-08
- 0.1.0 — 2024-09-08
- 0.0.22 — 2024-07-08
- … 21 more at https://npm.io/package/cssx-framework/versions

## README

# Overview

CSSX is a web UI framework with file-path routing. You've heard of CSS-in-JS, and CSS pre/post-processors like SASS, LESS, and Tailwind - but now get ready for a whole new world of CSS driven development, with CSSX!

## Why?

I built CSS because I thought it was an entertaining idea and would parallel the million JS frameworks that exist. It doesn't really have a purpose except to entertain and see if it was possible.

## Getting Started

From the terminal, run `npx create-cssx@latest` or `npm create cssx@latest`, and follow the prompts to set up a basic template. From there run `(npm | yarn | pnpm | bun) install` > `(npm | yarn | pnpm | bun) start` to spin up the local dev environment.

## Folder Structure

As CSSX is a file-path route based framework, the default template expects each page to be a `.cssx` file within a `./routes` directory. This can be updated by changing the `routeDir` in the `cssx.json` file.

Any mixins or shared components that are setup can be stored however you like, and then referenced as imports (`@import '{relative_path_to_file}';`) relative to the calling file.

## Code Structure

### Overview

`.cssx` files have roughly the same format as a standard `.css` file, but with some additional functions like mixins in order to provide functionality for shared component styling.

To render elements on the page, simply write the CSS you require, but include the element tag in the specificity of the class (ie. `div.root` would render a `div` element with the class `root`). Any element attributes you wish to include can be added in the same format as CSS variables nested within the class definition.

In order to define nested elements, just nest the CSS class definitions under the appropriate parent, like you would structure regular CSS.

eg.

```css
div.root {
    color: red;
    --title: Div Title;
    --text: Some text;

    button {
        border-radius: 8px;
        color: blue;
        --onclick: alert('Button clicked!');
        --text: Click me;
        --type: button;
    }
}
```

would become =>

```html
<div class="root" title="Div Title">
    Some text
    <button onclick="alert('Button clicked!')" type="button">
        Click me
    </button>
</div>
```

with the stylesheet

```css
div.root {
    color: red;

    button {
        border-radius: 8px;
        color: blue;
    }
}
```

### Mixins / Shared Components

Mixins can be defined with the syntax below:

```scss
@mixin mixinName($param1: defaultValue1, $param2: defaultValue2) {
    ...
}
```

and referenced as an `@include` statement: `@include mixinName();` (For brevity, the parentheses can be omitted if empty `@include mixinName;`)

Mixin parameters can be overridden when including in a component, but if defining overrides then all parameters must be defined - ie. either define all of them, or leave them empty. If not all parameters are to be overridden, then the value `default` can be used as a placeholder: `@include mixinName(default, overrideValue);`

A mixin can either be defined as a full component, with the element definition at the top level:

```scss
@mixin standardButton($backgroundColor: lightgreen, $hoverColor: lightblue, $class: example, $borderRadius: 6px, $type: button) {
    button {
        background-color: $backgroundColor;
        --borderRadius: $borderRadius;
        --class: $class;
        --type: $type;

        &:hover {
            background-color: $hoverColor;
        }
    }
}

div.root {
    @include standardButton;
}
```

or just as the nested styles / attributes / children, depending how you wish to interact with them

```scss
@mixin standardButton($backgroundColor: lightgreen, $hoverColor: lightblue, $class: example, $borderRadius: 6px, $type: button) {
    background-color: $backgroundColor;
    --borderRadius: $borderRadius;
    --class: $class;
    --type: $type;

    &:hover {
        background-color: $hoverColor;
    }
}

div.root {
    button {
        @include standardButton;
    }
}
```

### Event functions

Javascript functions can be defined inline for components, and will be transpiled to the appropriate element event attribute.

```css
button.aButton {
    --onclick: "console.log('test');";
    --type: "button";
    --text: "Click me";
}
```

=> 

```html
<button type="button" onclick="console.log('test');">
    Click me
</button>
```

Defining functions across multiple lines is also possible by defining the event attribute values on lines subsequent to the event attribute definition, as outlined in the snippet below. This will then get transpiled into a single line concatenated string on the event attribute.

> Note: When doing multiline functions, don't forget your semi-colons to break separate functions up - CSSX just joins the strings with spaces and isn't smart enough to know where semi-colons should go!

```scss
button.anotherButton {
    --onclick:
        "console.log('test');"
        "setInterval(() => {"
        "document.getElementById('picture-gallery').scrollBy({left: -25, behavior: 'smooth'})"
        "}, 100)";
    --text: "Click me again"
}
```

=>

```html
<button class="anotherButton" onclick="console.log('test'); setInterval(() => { document.getElementById('picture-gallery').scrollBy({left: -25, behavior: 'smooth'}) }, 100)">
    Click me again
</button>
```

To make shared functions, simply pull your event attribute definitions out into a mixin, and `@include` them like a normal mixin.

> Note: Mixin params are determined at **compile time**, so ensure that your mixin functions cater for this.

```scss
@mixin testFunction() {
    --onclick:
        "console.log('test');"
        "setInterval(() => {"
        "document.getElementById('picture-gallery').scrollBy({left: -25, behavior: 'smooth'})"
        "}, 100)";
}

button.anotherButton {
    @include testFunction;
    --text: "Click me again";
}
```

It is also possible to further abstract functions by using a mixin parameter to define which attribute the function fires on. This can be overridden when including the function on an element and provides further flexibility in function definitions.

```scss
@mixin testFunction($attribute: onclick) {
    --$attribute:
        "console.log('test');"
        "setInterval(() => {"
        "document.getElementById('picture-gallery').scrollBy({left: -25, behavior: 'smooth'})"
        "}, 100)";
}

button.anotherButton {
    @include testFunction(onload);
    --text: "Click me again";
}
```

#### Built-in Function Variables

CSSX has some built-in function variables to simplify the code needed for event functions. They can be referenced for any event function and will be replaced via string replacement at compile time. The functionality of these built-in functions can be overidden by redefining a variable with the same name within your CSSX.

These are defined below:

```js
const BUILT_IN_FUNCTIONS = {
  '$setVariable': 'document.styleSheets[document.styleSheets.length - 1].cssRules[0].style.setProperty',
  '$getVariable': 'document.styleSheets[document.styleSheets.length - 1].cssRules[0].style.getPropertyValue',
  '$parseObject': 'JSON.parse',
  '$stringifyObject': 'JSON.stringify',
}
```

Example:

```scss
button {
    --onclick: $getVariable('--object');
    --text: "Click";
    --type: "button";
}
```

=>

```html
<button onclick="document.styleSheets[0].cssRules[0].style.getPropertyValue('--object')" type="button">
    Click
</button>
```

### Complex objects

All variables used to interact with the UI in CSSX are managed as CSS variables (ie `--variable: value;`), and need to be defined as **immediate children of the root object**. As CSSX is a line-by-line framework, and isn't smart enough to understand complex object variables, these need to be defined as single line string values as well (ie `--form: {"name": null, "email": null};`).

In order to perform any object manipulation, these variables will then need to be parsed to an object first.

An example for alerting an object might be:

```scss
div.page {
    --form: {"name": null, "email": null};

    button {
        --onclick: alert($parseObject($getVariable('--form')));
        --type: "button";
    }
}
```

---
_Source: https://npm.io/package/cssx-framework · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
