-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Existing Effects & Refs #196
Changes from 9 commits
2bcf119
f630a6d
f013dfa
c1ecb57
b08454d
25fb027
6df4dd4
129944c
9052827
5ac60bf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { registerHtml, useEffect, TramOneComponent } from '../../src/tram-one'; | ||
|
||
const html = registerHtml(); | ||
|
||
/** | ||
* Element with a use effect (to test behavior when wrapped) | ||
*/ | ||
const anchorSet: TramOneComponent = () => { | ||
useEffect(() => { | ||
window.location.hash = 'testing'; | ||
}); | ||
return html`<section />`; | ||
}; | ||
|
||
export default anchorSet; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { registerHtml, useUrlParams, TramOneComponent } from '../../src/tram-one'; | ||
import anchorSet from './anchor-set'; | ||
|
||
const html = registerHtml({ | ||
'anchor-set': anchorSet, | ||
}); | ||
|
||
/** | ||
* Element to test behavior or wrapping a single component | ||
*/ | ||
const anchors: TramOneComponent = () => { | ||
if (useUrlParams().hash !== 'testing') return html`<anchor-set />`; | ||
return html`<section>Anchor Set</section>`; | ||
}; | ||
|
||
export default anchors; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { registerHtml, useEffect, TramOneComponent } from '../../src/tram-one'; | ||
|
||
const html = registerHtml(); | ||
|
||
/** | ||
* Element to test useEffect reference point | ||
*/ | ||
const focusInput: TramOneComponent = () => { | ||
useEffect((ref) => { | ||
ref.focus(); | ||
}); | ||
return html`<input placeholder="Input for automatic focus" />`; | ||
}; | ||
|
||
export default focusInput; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,6 @@ export default (component: RootTramOneComponent, container: Container) => { | |
|
||
// this sadly needs to be wrapped in some element so we can process effects | ||
// otherwise the root node will not have effects applied on it | ||
const renderedApp = html`<div><app /></div>`; | ||
const renderedApp = html`<tram-one><app /></tram-one>`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
container.replaceChild(renderedApp, container.firstElementChild); | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ import { | |
TRAM_TAG_STORE_KEYS, | ||
} from './node-names'; | ||
import { buildNamespace } from './namespace'; | ||
import { TramOneElement } from './types'; | ||
import { CleanupEffect, TramOneElement, TramOneHTMLElement, TramOneSVGElement } from './types'; | ||
import { getObservableStore } from './observable-store'; | ||
import { TRAM_OBSERVABLE_STORE, TRAM_KEY_STORE } from './engine-names'; | ||
import { decrementKeyStoreValue, getKeyStore, incrementKeyStoreValue } from './key-store'; | ||
|
@@ -25,7 +25,7 @@ import { decrementKeyStoreValue, getKeyStore, incrementKeyStoreValue } from './k | |
* process side-effects for new tram-one nodes | ||
* (this includes calling effects, and keeping track of stores) | ||
*/ | ||
const processTramTags = (node: Node | TramOneElement) => { | ||
const processTramTags = (node: Node | (TramOneHTMLElement | TramOneSVGElement)) => { | ||
// if this element doesn't have a TRAM_TAG, it's not a Tram-One Element | ||
if (!(TRAM_TAG in node)) { | ||
return; | ||
|
@@ -55,7 +55,7 @@ const processTramTags = (node: Node | TramOneElement) => { | |
const effectReaction = observe(() => { | ||
// verify that cleanup is a function before calling it (in case it was a promise) | ||
if (typeof cleanup === 'function') cleanup(); | ||
cleanup = effect(); | ||
cleanup = effect(node); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is what is required for allowing us to accept ref as a parameter for |
||
}); | ||
|
||
// this is called when a component with an effect is removed | ||
|
@@ -76,7 +76,7 @@ const processTramTags = (node: Node | TramOneElement) => { | |
/** | ||
* call all cleanup effects on the node | ||
*/ | ||
const cleanupEffects = (cleanupEffects: (() => void)[]) => { | ||
const cleanupEffects = (cleanupEffects: CleanupEffect[]) => { | ||
cleanupEffects.forEach((cleanup) => cleanup()); | ||
}; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,10 +21,10 @@ export type ElementOrSelector = [string | HTMLElement][0]; | |
* Type for our template renderers (either html or svg). | ||
* This is not wrapped in an indexed alias, because everything should be provided automatically. | ||
*/ | ||
export type DOMTaggedTemplateFunction = ( | ||
export type DOMTaggedTemplateFunction<TramOneElementType extends TramOneElement> = ( | ||
strings: TemplateStringsArray, | ||
...elementsAndAttributes: any[] | ||
) => TramOneElement; | ||
) => TramOneElementType; | ||
|
||
/** | ||
* Type for custom Tram One Components. | ||
|
@@ -54,7 +54,7 @@ export type CleanupEffect = [() => unknown][0]; | |
* Type for the effect function. | ||
* This is passed into the useEffect hook | ||
*/ | ||
export type Effect = [() => unknown][0]; | ||
export type Effect = [(ref: TramOneHTMLElement | TramOneSVGElement) => unknown][0]; | ||
|
||
/** | ||
* The Props interface for custom Tram One Components. | ||
|
@@ -108,6 +108,9 @@ export interface TramOneElement extends Element { | |
[TRAM_TAG_STORE_KEYS]: string[]; | ||
} | ||
|
||
export type TramOneHTMLElement = TramOneElement & HTMLElement; | ||
export type TramOneSVGElement = TramOneElement & SVGElement; | ||
|
||
Comment on lines
+111
to
+113
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. new type definitions, because TramOneElement is too generic, and in reality (right now) it's always an HTMLElement or SVGElement. If we ever expose registerDOM, people should be able to make their own TramOneElement definitions (which shouldn't be too hard to do). |
||
/** | ||
* Type for the Root TramOneComponent, | ||
* it can have no props or children, since it is the root element | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,7 @@ import { UrlMatchResults } from './types'; | |
* | ||
* @returns object with a `matches` key, and (if it matched) path and query parameters | ||
*/ | ||
export default (pattern: string): UrlMatchResults => { | ||
export default (pattern?: string): UrlMatchResults => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should have always been optional! This better matches the interface of the underlying library. |
||
// save and update results in an observable, so that we can update | ||
// components and effects in a reactive way | ||
const initialParams = useUrlParams(pattern) as UrlMatchResults; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there's no reason this should have been populated by the existing new effects before...