This repository has been archived by the owner on Aug 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9e05378
commit d2ab47d
Showing
12 changed files
with
185 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,16 @@ | ||
/** | ||
* @typedef NodePart { import('../parts.js').NodePart } | ||
* @typedef Part { import('../parts.js').Part } | ||
*/ | ||
import { directive, isNodePart } from '../index.js'; | ||
|
||
export const asyncAppend = directive(asyncAppendDirective); | ||
|
||
/** | ||
* Render items of an AsyncIterable | ||
* | ||
* @param { AsyncIterableIterator } value | ||
* @returns { (part: NodePart) => void } | ||
* @type { (value: AsyncIterable<unknown>) => (part: Part) => void } | ||
*/ | ||
function asyncAppendDirective(value) { | ||
return function(part) { | ||
if (!isNodePart(part)) { | ||
throw Error('The `asyncAppend` directive can only be used in text nodes'); | ||
} | ||
part.setValue(value); | ||
}; | ||
} | ||
export const asyncAppend = directive((value) => (part) => { | ||
if (!isNodePart(part)) { | ||
throw Error('The `asyncAppend` directive can only be used in text nodes'); | ||
} | ||
part.setValue(value); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,17 @@ | ||
/** | ||
* @typedef NodePart { import('../parts.js').NodePart } | ||
* @typedef Part { import('../parts.js').Part } | ||
*/ | ||
import { directive, isNodePart } from '../index.js'; | ||
|
||
export const cache = directive(cacheDirective); | ||
|
||
/** | ||
* Enables fast switching between multiple templates by caching previous results. | ||
* Not possible/desireable to cache between server-side requests, so this is a no-op. | ||
* | ||
* @param { unknown } value | ||
* @returns { (part: NodePart) => void } | ||
* @type { (value: unknown) => (part: Part) => void } | ||
*/ | ||
function cacheDirective(value) { | ||
return function(part) { | ||
if (!isNodePart(part)) { | ||
throw Error('The `cache` directive can only be used in text nodes'); | ||
} | ||
part.setValue(value); | ||
}; | ||
} | ||
export const cache = directive((value) => (part) => { | ||
if (!isNodePart(part)) { | ||
throw Error('The `cache` directive can only be used in text nodes'); | ||
} | ||
part.setValue(value); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,26 @@ | ||
/** | ||
* @typedef Part { import('../parts.js').Part } | ||
*/ | ||
import { directive, isAttributePart } from '../index.js'; | ||
|
||
export const classMap = directive(classMapDirective); | ||
|
||
/** | ||
* Applies CSS classes, where'classInfo' keys are added as class names if values are truthy. | ||
* Only applies to 'class' attribute. | ||
* | ||
* @param { object } classInfo | ||
* @returns { (part: AttributePart) => void } | ||
* @type { (classInfo: { [name: string]: string | boolean | number }) => (part: Part) => void } | ||
*/ | ||
function classMapDirective(classInfo) { | ||
return function(part) { | ||
if (!isAttributePart(part) || part.name !== 'class') { | ||
throw Error('The `classMap` directive can only be used in the `class` attribute'); | ||
} | ||
export const classMap = directive((classInfo) => (part) => { | ||
if (!isAttributePart(part) || part.name !== 'class') { | ||
throw Error('The `classMap` directive can only be used in the `class` attribute'); | ||
} | ||
|
||
let value = ''; | ||
let value = ''; | ||
|
||
for (const key in classInfo) { | ||
if (classInfo[key]) { | ||
value += `${value.length ? ' ' : ''}${key}`; | ||
} | ||
for (const key in classInfo) { | ||
if (classInfo[key]) { | ||
value += `${value.length ? ' ' : ''}${key}`; | ||
} | ||
} | ||
|
||
part.setValue(value); | ||
}; | ||
} | ||
part.setValue(value); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,15 @@ | ||
/** | ||
* @typedef Part { import('../parts.js').Part } | ||
*/ | ||
import { directive } from '../index.js'; | ||
|
||
export const guard = directive(guardDirective); | ||
|
||
/** | ||
* Guard against re-render. | ||
* Not possible to compare against previous render in a server context, | ||
* so this is a no-op. | ||
* | ||
* @param { unknown } value | ||
* @param { () => unknown } fn | ||
* @returns { (part: NodePart) => void } | ||
* @type { (value: unknown, fn: () => unknown) => (part: Part) => void } | ||
*/ | ||
function guardDirective(value, fn) { | ||
return function(part) { | ||
part.setValue(fn()); | ||
}; | ||
} | ||
export const guard = directive((value, fn) => (part) => { | ||
part.setValue(fn()); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,17 @@ | ||
/** | ||
* @typedef Part { import('../parts.js').Part } | ||
*/ | ||
import { directive, isAttributePart, nothingString } from '../index.js'; | ||
|
||
export const ifDefined = directive(ifDefinedDirective); | ||
|
||
/** | ||
* Sets the attribute if 'value' is defined, | ||
* removes the attribute if undefined. | ||
* | ||
* @param { unknown } value | ||
* @returns { (part: AttributePart) => void } | ||
* @type { (value: unknown) => (part: Part) => void } | ||
*/ | ||
function ifDefinedDirective(value) { | ||
return function(part) { | ||
if (value === undefined && isAttributePart(part)) { | ||
return part.setValue(nothingString); | ||
} | ||
part.setValue(value); | ||
}; | ||
} | ||
export const ifDefined = directive((value) => (part) => { | ||
if (value === undefined && isAttributePart(part)) { | ||
return part.setValue(nothingString); | ||
} | ||
part.setValue(value); | ||
}); |
Oops, something went wrong.