### Fez Test Structure Example
Source: https://github.com/dux/fez/blob/main/test/README.md
A JavaScript code snippet demonstrating how to write a new test for the Fez library, including component setup and state assertion.
```javascript
test('My new test', () => {
class MyComponent extends FezBase {
init() {
this.state.value = 'initial';
}
}
const component = new MyComponent();
component._node = new window.HTMLElement();
component.root = component._node;
component.class = MyComponent;
component.fezRegister();
component.init();
assert(component.state.value === 'initial', 'State should be initialized');
});
```
--------------------------------
### FAST Rendering Configuration in Fez
Source: https://github.com/dux/fez/blob/main/AI_FEZ_GUIDE.md
Provides examples of how to configure `FAST = true` for components that do not use slots, preventing render flicker, and conditional fast rendering based on node attributes.
```javascript
// Always fast (no slots)
class { FAST = true }
// Conditional fast rendering
class {
FAST = (node) => node.hasAttribute('title') // Fast if has title, slow if needs innerHTML
}
```
--------------------------------
### FEZ Fetch API - Basic Usage
Source: https://github.com/dux/fez/blob/main/README.md
Illustrates the basic usage of the Fez.fetch API for making GET and POST requests. It shows how to use promises with async/await for handling responses and callbacks for asynchronous processing, highlighting flexible parameter ordering.
```js
// GET request with promise
const data = await Fez.fetch('https://api.example.com/data')
// GET request with callback, does not create promise
Fez.fetch('https://api.example.com/data', (data) => {
console.log(data)
})
// POST request
const result = await Fez.fetch('POST', 'https://api.example.com/data', { key: 'value' })
```
--------------------------------
### Create and Use a Fez Counter Component
Source: https://github.com/dux/fez/blob/main/index.html
Demonstrates how to create a custom 'ui-counter' component using Fez and then use it within the HTML body. The component can accept a 'start' attribute to initialize its value.
```html
```
--------------------------------
### Dux Fez Task List Rendering and Controls
Source: https://github.com/dux/fez/blob/main/test/tpl.html
This snippet shows how tasks are rendered in the Dux Fez template. It includes conditional rendering for when no tasks are found, iterating through tasks to display their status and action buttons (complete/remove), and provides buttons for adding a new task and clearing completed tasks. It also displays the raw state of the tasks.
```html
{#if !@state.tasks[0]}
No tasks found
{/if}
{#for task, index in @state.tasks}
{#if task.animate}
{else}
{/if}
⋅ ⋅ ×
{/for}
add task ⋅ clear completed
{ JSON.stringify(this.state.tasks, null, 2) }
```
--------------------------------
### Task Management Methods in Dux Fez
Source: https://github.com/dux/fez/blob/main/test/tpl.html
Provides methods for managing tasks within the Dux Fez framework. These methods include toggling task completion, clearing all completed tasks, removing a specific task, updating a task's name, adding a new task with animation, and initializing the task list with predefined tasks.
```javascript
toggleComplete(index) {
const task = this.state.tasks[index]
task.done = !task.done
}
clearCompleted() {
this.state.tasks = this.state.tasks.filter((t) => !t.done)
}
removeTask(index) {
this.state.tasks = this.state.tasks.filter((_, i) => i !== index);
}
setName(index, name) {
this.state.tasks[index].name = name
}
addTask() {
this.counter ||= 0
this.state.tasks.push({ name: `new task ${++this.counter}`, done: false, animate: true })
}
animate(node) {
$(node)
.css('display', 'block')
.animate({height: '33px', opacity: 1}, 200, () => {
delete this.state.tasks[this.state.tasks.length - 1].animate
$(node).css('height', 'auto')
})
}
init() {
this.state.tasks = [
{name: 'First task', done: false},
{name: 'Second task', done: false},
{name: 'Third task', done: true},
]
}
```
--------------------------------
### Define a Counter Component with FEZ
Source: https://github.com/dux/fez/blob/main/README.md
Example of a custom counter component using FEZ, demonstrating reactive state, event handling, conditional rendering, and scoped SCSS.
```html
{{ state.count }}
{{if state.count > 0}}
—
{{if state.count == MAX }}
MAX
{{else}}
{{#if state.count % 2 }}
odd
{{else}}
even
{{/if}}
{{/if}}
{{/if}}
```
--------------------------------
### Shared Counter Example Using Global State
Source: https://github.com/dux/fez/blob/main/README.md
This example demonstrates how multiple Fez counter components can share and synchronize a global maximum count. Each counter updates the global `maxCount`, ensuring all instances reflect the highest count achieved across all components.
```javascript
// Multiple counter components sharing max count
class Counter extends FezBase {
init(props) {
this.state.count = parseInt(props.start || 0)
}
beforeRender() {
// All counters share and update the global max
this.globalState.maxCount ||= 0
// Find max across all counter instances
let max = 0
Fez.state.forEach('maxCount', fez => {
if (fez.state?.count > max) {
max = fez.state.count
}
})
this.globalState.maxCount = max
}
render() {
return `
Count: ${this.state.count}(Global max: ${this.globalState.maxCount})
`
}
}
```
--------------------------------
### Dynamic Fill Control Example
Source: https://github.com/dux/fez/blob/main/demo/fez/ui-star.html
Illustrates controlling the fill percentage of the star rating component dynamically. This is typically done via JavaScript or data binding.
```html
```
--------------------------------
### Styling the Star Rating Component
Source: https://github.com/dux/fez/blob/main/demo/fez/ui-star.html
Provides an example of CSS styling for the star rating component, specifically adding margin between stars.
```css
.fez-ui-star {
margin-right: 5px;
}
```
--------------------------------
### Get First-Level Child Nodes (FEZ Instance Helper)
Source: https://github.com/dux/fez/blob/main/demo/fez/ui-tabs.html
The `this.childNodes()` function is a helper within the FEZ instance designed to retrieve all direct child nodes. It specifically excludes text nodes (#text) from the result set, providing a clean list of element-based children. This is useful for iterating over immediate descendants without needing to filter text nodes manually.
```javascript
this.childNodes()
```
--------------------------------
### Fez Component Structure with ES Module Imports and Dynamic Loading
Source: https://github.com/dux/fez/blob/main/AI_FEZ_GUIDE.md
Demonstrates the basic structure of a Fez component, including optional ES Module imports, dynamic loading of JavaScript and CSS, FAST rendering control, and lifecycle methods like init(), onMount(), onStateChange(), onDestroy(), onWindowResize(), and onWindowScroll().
```html
```
--------------------------------
### Initializing Props in Fez Components (JavaScript)
Source: https://github.com/dux/fez/blob/main/AI_FEZ_GUIDE.md
Shows how to access and utilize props within the `init` method of a Fez component, including setting default values and handling function props.
```javascript
init(props) {
this.state.font_size = props.font_size || 24
this.state.background_color = props.background_color || '#000'
this.state.is_active = props.is_active !== undefined
// Function props are already resolved
if (props.onselect) {
this.onSelectHandler = props.onselect
}
}
```
--------------------------------
### Importing External Libraries in Fez
Source: https://github.com/dux/fez/blob/main/AI_FEZ_GUIDE.md
Shows how to import ES Modules from CDNs using the `/+esm` path and how to dynamically load external JavaScript and CSS files into the Fez application's head.
```javascript
// ES Module imports (use /+esm for CDN modules)
import library from 'https://cdn.jsdelivr.net/npm/library/+esm'
// Dynamic script/style loading
Fez.head({js: 'https://cdn.example.com/script.js'})
Fez.head({css: 'https://cdn.example.com/styles.css'})
```
--------------------------------
### Fez Event Handling: Component Methods and Child Access
Source: https://github.com/dux/fez/blob/main/AI_FEZ_GUIDE.md
Demonstrates how to handle events in Fez components, including calling component methods directly and accessing methods from child components using the Fez constructor.
```html