marko 5.37.5 → 6.0.0-3.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/common/attr-tag.d.ts +8 -0
- package/dist/common/compat-meta.d.ts +2 -0
- package/dist/common/for.d.ts +5 -0
- package/dist/common/helpers.d.ts +7 -0
- package/dist/common/meta.d.ts +2 -0
- package/dist/common/types.d.ts +105 -0
- package/dist/debug/dom.js +2111 -0
- package/dist/debug/dom.mjs +2088 -0
- package/dist/debug/html.js +2740 -0
- package/dist/debug/html.mjs +2663 -0
- package/dist/dom/abort-signal.d.ts +3 -0
- package/dist/dom/compat.d.ts +20 -0
- package/dist/dom/control-flow.d.ts +14 -0
- package/dist/dom/controllable.d.ts +12 -0
- package/dist/dom/dom.d.ts +19 -0
- package/dist/dom/event.d.ts +4 -0
- package/dist/dom/parse-html.d.ts +5 -0
- package/dist/dom/queue.d.ts +10 -0
- package/dist/dom/reconcile.d.ts +2 -0
- package/dist/dom/renderer.d.ts +21 -0
- package/dist/dom/resolve-cursor-position.d.ts +1 -0
- package/dist/dom/resume.d.ts +9 -0
- package/dist/dom/schedule.d.ts +2 -0
- package/dist/dom/scope.d.ts +7 -0
- package/dist/dom/signals.d.ts +39 -0
- package/dist/dom/template.d.ts +3 -0
- package/dist/dom/walker.d.ts +4 -0
- package/dist/dom.d.ts +15 -0
- package/dist/dom.js +1370 -0
- package/dist/dom.mjs +1350 -0
- package/dist/html/attrs.d.ts +14 -0
- package/dist/html/compat.d.ts +14 -0
- package/dist/html/content.d.ts +5 -0
- package/dist/html/dynamic-tag.d.ts +12 -0
- package/dist/html/for.d.ts +4 -0
- package/dist/html/inlined-runtimes.d.ts +2 -0
- package/dist/html/serializer.d.ts +14 -0
- package/dist/html/template.d.ts +3 -0
- package/dist/html/writer.d.ts +114 -0
- package/dist/html.d.ts +9 -0
- package/dist/html.js +1818 -0
- package/dist/html.mjs +1744 -0
- package/dist/translator/core/attrs.d.ts +3 -0
- package/dist/translator/core/client.d.ts +14 -0
- package/dist/translator/core/const.d.ts +3 -0
- package/dist/translator/core/debug.d.ts +3 -0
- package/dist/translator/core/define.d.ts +3 -0
- package/dist/translator/core/effect.d.ts +3 -0
- package/dist/translator/core/export.d.ts +3 -0
- package/dist/translator/core/for.d.ts +67 -0
- package/dist/translator/core/html-comment.d.ts +12 -0
- package/dist/translator/core/html-script.d.ts +9 -0
- package/dist/translator/core/html-style.d.ts +9 -0
- package/dist/translator/core/id.d.ts +3 -0
- package/dist/translator/core/if.d.ts +53 -0
- package/dist/translator/core/import.d.ts +3 -0
- package/dist/translator/core/index.d.ts +152 -0
- package/dist/translator/core/let.d.ts +8 -0
- package/dist/translator/core/lifecycle.d.ts +10 -0
- package/dist/translator/core/log.d.ts +3 -0
- package/dist/translator/core/return.d.ts +6 -0
- package/dist/translator/core/script.d.ts +3 -0
- package/dist/translator/core/server.d.ts +14 -0
- package/dist/translator/core/static.d.ts +14 -0
- package/dist/translator/core/style.d.ts +3 -0
- package/dist/translator/index.d.ts +201 -0
- package/dist/translator/index.js +9134 -490
- package/dist/translator/util/assert.d.ts +3 -0
- package/dist/translator/util/binding-has-downstream-expressions.d.ts +2 -0
- package/dist/translator/util/entry-builder.d.ts +19 -0
- package/dist/translator/util/evaluate.d.ts +11 -0
- package/dist/translator/util/for-each-identifier.d.ts +2 -0
- package/dist/translator/util/get-defined-binding-expression.d.ts +3 -0
- package/dist/translator/util/get-known-attr-values.d.ts +2 -0
- package/dist/translator/util/get-parent-tag.d.ts +2 -0
- package/dist/translator/util/get-root.d.ts +7 -0
- package/dist/translator/util/get-tag-name.d.ts +2 -0
- package/dist/translator/util/is-core-tag.d.ts +10 -0
- package/dist/translator/util/is-stateful.d.ts +3 -0
- package/dist/translator/util/is-static.d.ts +2 -0
- package/dist/translator/util/marko-config.d.ts +4 -0
- package/dist/translator/util/nested-attribute-tags.d.ts +17 -0
- package/dist/translator/util/normalize-string-expression.d.ts +3 -0
- package/dist/translator/util/optional.d.ts +20 -0
- package/dist/translator/util/plugin-hooks.d.ts +4 -0
- package/dist/translator/util/references.d.ts +84 -0
- package/dist/translator/util/runtime-info.d.ts +5 -0
- package/dist/translator/util/runtime.d.ts +15 -0
- package/dist/translator/util/scope-read.d.ts +6 -0
- package/dist/translator/util/sections.d.ts +51 -0
- package/dist/translator/util/signals.d.ts +61 -0
- package/dist/translator/util/simplify-fn.d.ts +5 -0
- package/dist/translator/util/state.d.ts +3 -0
- package/dist/translator/util/tag-name-type.d.ts +16 -0
- package/dist/translator/util/to-first-expression-or-block.d.ts +3 -0
- package/dist/translator/util/to-first-statement-or-block.d.ts +2 -0
- package/dist/translator/util/to-property-name.d.ts +7 -0
- package/dist/translator/util/translate-attrs.d.ts +12 -0
- package/dist/translator/util/translate-var.d.ts +2 -0
- package/dist/translator/util/traverse.d.ts +9 -0
- package/dist/translator/util/visitors.d.ts +11 -0
- package/dist/translator/util/walks.d.ts +15 -0
- package/dist/translator/util/with-previous-location.d.ts +2 -0
- package/dist/translator/util/writer.d.ts +13 -0
- package/dist/translator/visitors/cdata.d.ts +7 -0
- package/dist/translator/visitors/comment.d.ts +7 -0
- package/dist/translator/visitors/declaration.d.ts +7 -0
- package/dist/translator/visitors/document-type.d.ts +7 -0
- package/dist/translator/visitors/function.d.ts +19 -0
- package/dist/translator/visitors/import-declaration.d.ts +8 -0
- package/dist/translator/visitors/placeholder.d.ts +22 -0
- package/dist/translator/visitors/program/dom.d.ts +7 -0
- package/dist/translator/visitors/program/html.d.ts +8 -0
- package/dist/translator/visitors/program/index.d.ts +39 -0
- package/dist/translator/visitors/referenced-identifier.d.ts +7 -0
- package/dist/translator/visitors/scriptlet.d.ts +8 -0
- package/dist/translator/visitors/tag/attribute-tag.d.ts +11 -0
- package/dist/translator/visitors/tag/custom-tag.d.ts +19 -0
- package/dist/translator/visitors/tag/dynamic-tag.d.ts +18 -0
- package/dist/translator/visitors/tag/index.d.ts +15 -0
- package/dist/translator/visitors/tag/native-tag.d.ts +25 -0
- package/dist/translator/visitors/text.d.ts +7 -0
- package/package.json +34 -70
- package/tag-types/const.d.marko +7 -0
- package/tag-types/debug.d.marko +5 -0
- package/tag-types/define.d.marko +5 -0
- package/tag-types/do.d.marko +5 -0
- package/tag-types/effect.d.marko +5 -0
- package/tag-types/id.d.marko +3 -0
- package/tag-types/let.d.marko +8 -0
- package/tag-types/lifecycle.d.marko +7 -0
- package/tag-types/log.d.marko +5 -0
- package/tag-types/script.d.marko +5 -0
- package/README.md +0 -161
- package/bin/markoc +0 -2
- package/bin/markoc.js +0 -441
- package/browser-refresh.js +0 -5
- package/compiler-browser.marko +0 -15
- package/compiler.js +0 -7
- package/components-browser.marko +0 -15
- package/components.js +0 -7
- package/dist/build.json +0 -3
- package/dist/compiler/config.js +0 -45
- package/dist/compiler/index.js +0 -164
- package/dist/compiler/modules.js +0 -5
- package/dist/core-tags/components/init-components-tag.js +0 -54
- package/dist/core-tags/components/preferred-script-location-tag.js +0 -25
- package/dist/core-tags/components/preserve-tag.js +0 -1
- package/dist/core-tags/core/__flush_here_and_after__.js +0 -44
- package/dist/core-tags/core/await/AsyncValue.js +0 -121
- package/dist/core-tags/core/await/client-reorder-runtime.js +0 -51
- package/dist/core-tags/core/await/index.d.marko +0 -10
- package/dist/core-tags/core/await/renderer.js +0 -249
- package/dist/core-tags/core/await/reorderer-renderer.js +0 -156
- package/dist/index.js +0 -4
- package/dist/node-require/browser-refresh.js +0 -1
- package/dist/node-require/hot-reload.js +0 -45
- package/dist/node-require/index.js +0 -103
- package/dist/node_modules/@internal/components-beginComponent/index-browser.js +0 -23
- package/dist/node_modules/@internal/components-beginComponent/index.js +0 -84
- package/dist/node_modules/@internal/components-beginComponent/package.json +0 -11
- package/dist/node_modules/@internal/components-define-widget-legacy/index-browser.js +0 -335
- package/dist/node_modules/@internal/components-define-widget-legacy/index.js +0 -18
- package/dist/node_modules/@internal/components-define-widget-legacy/package.json +0 -11
- package/dist/node_modules/@internal/components-endComponent/index-browser.js +0 -5
- package/dist/node_modules/@internal/components-endComponent/index.js +0 -11
- package/dist/node_modules/@internal/components-endComponent/package.json +0 -11
- package/dist/node_modules/@internal/components-entry/index-browser.js +0 -11
- package/dist/node_modules/@internal/components-entry/index.js +0 -301
- package/dist/node_modules/@internal/components-entry/package.json +0 -11
- package/dist/node_modules/@internal/components-entry-legacy/index-browser.js +0 -76
- package/dist/node_modules/@internal/components-entry-legacy/index.js +0 -16
- package/dist/node_modules/@internal/components-entry-legacy/package.json +0 -11
- package/dist/node_modules/@internal/components-registry/index-browser.js +0 -586
- package/dist/node_modules/@internal/components-registry/index.js +0 -44
- package/dist/node_modules/@internal/components-registry/package.json +0 -11
- package/dist/node_modules/@internal/components-util/index-browser.js +0 -152
- package/dist/node_modules/@internal/components-util/index.js +0 -71
- package/dist/node_modules/@internal/components-util/package.json +0 -11
- package/dist/node_modules/@internal/create-readable/index-browser.js +0 -30
- package/dist/node_modules/@internal/create-readable/index.js +0 -57
- package/dist/node_modules/@internal/create-readable/package.json +0 -11
- package/dist/node_modules/@internal/loader/fallback-node.js +0 -89
- package/dist/node_modules/@internal/loader/index-browser.js +0 -2
- package/dist/node_modules/@internal/loader/index.js +0 -23
- package/dist/node_modules/@internal/loader/package.json +0 -10
- package/dist/node_modules/@internal/preserve-tag/index-browser.js +0 -46
- package/dist/node_modules/@internal/preserve-tag/index.js +0 -34
- package/dist/node_modules/@internal/preserve-tag/package.json +0 -11
- package/dist/node_modules/@internal/require/index-browser.js +0 -15
- package/dist/node_modules/@internal/require/index-legacy-browser.js +0 -20
- package/dist/node_modules/@internal/require/index-webpack.js +0 -16
- package/dist/node_modules/@internal/require/index.js +0 -15
- package/dist/node_modules/@internal/require/package.json +0 -11
- package/dist/node_modules/@internal/set-immediate/index-browser.js +0 -19
- package/dist/node_modules/@internal/set-immediate/index-worker.js +0 -31
- package/dist/node_modules/@internal/set-immediate/index.js +0 -3
- package/dist/node_modules/@internal/set-immediate/package.json +0 -11
- package/dist/node_modules/@internal/set-immediate/queueMicrotask.js +0 -8
- package/dist/runtime/RenderResult.js +0 -112
- package/dist/runtime/components/Component.js +0 -663
- package/dist/runtime/components/ComponentDef.js +0 -169
- package/dist/runtime/components/ComponentsContext.js +0 -59
- package/dist/runtime/components/GlobalComponentsContext.js +0 -10
- package/dist/runtime/components/KeySequence.js +0 -16
- package/dist/runtime/components/ServerComponent.js +0 -73
- package/dist/runtime/components/State.js +0 -101
- package/dist/runtime/components/attach-detach.js +0 -60
- package/dist/runtime/components/defineComponent.js +0 -60
- package/dist/runtime/components/dom-data.js +0 -8
- package/dist/runtime/components/event-delegation.js +0 -144
- package/dist/runtime/components/index.js +0 -1
- package/dist/runtime/components/legacy/browser.json +0 -9
- package/dist/runtime/components/legacy/defineComponent-legacy.js +0 -27
- package/dist/runtime/components/legacy/defineRenderer-legacy.js +0 -171
- package/dist/runtime/components/legacy/defineWidget-legacy.js +0 -1
- package/dist/runtime/components/legacy/dependencies/html.js +0 -3
- package/dist/runtime/components/legacy/dependencies/index.js +0 -177
- package/dist/runtime/components/legacy/dependencies/vdom.js +0 -3
- package/dist/runtime/components/legacy/index.js +0 -1
- package/dist/runtime/components/legacy/jquery.js +0 -52
- package/dist/runtime/components/legacy/ready.js +0 -152
- package/dist/runtime/components/legacy/renderer-legacy.js +0 -231
- package/dist/runtime/components/registry.js +0 -1
- package/dist/runtime/components/renderer.js +0 -236
- package/dist/runtime/components/update-manager.js +0 -94
- package/dist/runtime/createOut.js +0 -13
- package/dist/runtime/dom-insert.js +0 -77
- package/dist/runtime/events.js +0 -2
- package/dist/runtime/helpers/_change-case.js +0 -45
- package/dist/runtime/helpers/assign.js +0 -21
- package/dist/runtime/helpers/attr-tag.js +0 -43
- package/dist/runtime/helpers/class-value.js +0 -30
- package/dist/runtime/helpers/dynamic-tag.js +0 -181
- package/dist/runtime/helpers/empty-component.js +0 -1
- package/dist/runtime/helpers/merge.js +0 -13
- package/dist/runtime/helpers/of-fallback.js +0 -4
- package/dist/runtime/helpers/render-tag.js +0 -20
- package/dist/runtime/helpers/serialize-noop.js +0 -5
- package/dist/runtime/helpers/style-value.js +0 -45
- package/dist/runtime/helpers/tags-compat/dom-debug.js +0 -1
- package/dist/runtime/helpers/tags-compat/dom-debug.mjs +0 -4
- package/dist/runtime/helpers/tags-compat/dom.js +0 -1
- package/dist/runtime/helpers/tags-compat/dom.mjs +0 -4
- package/dist/runtime/helpers/tags-compat/html-debug.js +0 -3
- package/dist/runtime/helpers/tags-compat/html-debug.mjs +0 -4
- package/dist/runtime/helpers/tags-compat/html.js +0 -3
- package/dist/runtime/helpers/tags-compat/html.mjs +0 -4
- package/dist/runtime/helpers/tags-compat/runtime-dom.js +0 -239
- package/dist/runtime/helpers/tags-compat/runtime-html.js +0 -136
- package/dist/runtime/helpers/to-string.js +0 -5
- package/dist/runtime/html/AsyncStream.js +0 -805
- package/dist/runtime/html/BufferedWriter.js +0 -52
- package/dist/runtime/html/StringWriter.js +0 -73
- package/dist/runtime/html/helpers/_dynamic-attr.js +0 -35
- package/dist/runtime/html/helpers/attr.js +0 -80
- package/dist/runtime/html/helpers/attrs.js +0 -26
- package/dist/runtime/html/helpers/class-attr.js +0 -8
- package/dist/runtime/html/helpers/data-marko.js +0 -36
- package/dist/runtime/html/helpers/escape-quotes.js +0 -35
- package/dist/runtime/html/helpers/escape-script-placeholder.js +0 -24
- package/dist/runtime/html/helpers/escape-style-placeholder.js +0 -22
- package/dist/runtime/html/helpers/escape-xml.js +0 -21
- package/dist/runtime/html/helpers/merge-attrs.js +0 -55
- package/dist/runtime/html/helpers/props-script.js +0 -32
- package/dist/runtime/html/helpers/style-attr.js +0 -8
- package/dist/runtime/html/hot-reload.js +0 -26
- package/dist/runtime/html/index.js +0 -33
- package/dist/runtime/html/marko-namespace.js +0 -3
- package/dist/runtime/renderable.js +0 -194
- package/dist/runtime/vdom/AsyncVDOMBuilder.js +0 -456
- package/dist/runtime/vdom/VComment.js +0 -24
- package/dist/runtime/vdom/VComponent.js +0 -17
- package/dist/runtime/vdom/VDocumentFragment.js +0 -34
- package/dist/runtime/vdom/VElement.js +0 -375
- package/dist/runtime/vdom/VFragment.js +0 -26
- package/dist/runtime/vdom/VNode.js +0 -100
- package/dist/runtime/vdom/VText.js +0 -25
- package/dist/runtime/vdom/helpers/attrs.js +0 -62
- package/dist/runtime/vdom/helpers/const-element.js +0 -25
- package/dist/runtime/vdom/helpers/merge-attrs.js +0 -16
- package/dist/runtime/vdom/hot-reload.js +0 -115
- package/dist/runtime/vdom/index.js +0 -30
- package/dist/runtime/vdom/is-text-only.js +0 -10
- package/dist/runtime/vdom/marko-namespace.js +0 -3
- package/dist/runtime/vdom/morphdom/fragment.js +0 -95
- package/dist/runtime/vdom/morphdom/helpers.js +0 -42
- package/dist/runtime/vdom/morphdom/index.js +0 -741
- package/dist/runtime/vdom/parse-html.js +0 -18
- package/dist/runtime/vdom/vdom.js +0 -88
- package/dist/taglib/index.js +0 -51
- package/dist/translator/cdata/index.js +0 -15
- package/dist/translator/cdata/index[html].js +0 -15
- package/dist/translator/cdata/index[vdom].js +0 -12
- package/dist/translator/class.js +0 -65
- package/dist/translator/comment/index.js +0 -15
- package/dist/translator/comment/index[html].js +0 -17
- package/dist/translator/comment/index[vdom].js +0 -3
- package/dist/translator/declaration/index.js +0 -15
- package/dist/translator/declaration/index[html].js +0 -12
- package/dist/translator/declaration/index[vdom].js +0 -3
- package/dist/translator/document-type/index.js +0 -15
- package/dist/translator/document-type/index[html].js +0 -12
- package/dist/translator/document-type/index[vdom].js +0 -3
- package/dist/translator/placeholder/index.js +0 -15
- package/dist/translator/placeholder/index[html].js +0 -93
- package/dist/translator/placeholder/index[vdom].js +0 -22
- package/dist/translator/scriptlet.js +0 -4
- package/dist/translator/tag/attribute/directives/class.js +0 -42
- package/dist/translator/tag/attribute/directives/index.js +0 -15
- package/dist/translator/tag/attribute/directives/no-update-body-if.js +0 -15
- package/dist/translator/tag/attribute/directives/no-update-body.js +0 -10
- package/dist/translator/tag/attribute/directives/no-update-if.js +0 -12
- package/dist/translator/tag/attribute/directives/no-update.js +0 -48
- package/dist/translator/tag/attribute/directives/style.js +0 -42
- package/dist/translator/tag/attribute/index.js +0 -150
- package/dist/translator/tag/attribute/modifiers/index.js +0 -7
- package/dist/translator/tag/attribute/modifiers/no-update.js +0 -14
- package/dist/translator/tag/attribute/modifiers/scoped.js +0 -23
- package/dist/translator/tag/attribute-tag.js +0 -149
- package/dist/translator/tag/custom-tag.js +0 -146
- package/dist/translator/tag/dynamic-tag.js +0 -74
- package/dist/translator/tag/index.js +0 -287
- package/dist/translator/tag/macro-tag.js +0 -7
- package/dist/translator/tag/native-tag.js +0 -27
- package/dist/translator/tag/native-tag[html]/attributes.js +0 -151
- package/dist/translator/tag/native-tag[html]/index.js +0 -221
- package/dist/translator/tag/native-tag[vdom]/attributes.js +0 -105
- package/dist/translator/tag/native-tag[vdom]/index.js +0 -189
- package/dist/translator/tag/util.js +0 -246
- package/dist/translator/taglib/core/conditional/translate-else-if.js +0 -20
- package/dist/translator/taglib/core/conditional/translate-else.js +0 -20
- package/dist/translator/taglib/core/conditional/translate-if.js +0 -8
- package/dist/translator/taglib/core/conditional/util.js +0 -41
- package/dist/translator/taglib/core/index.js +0 -474
- package/dist/translator/taglib/core/macro/parse.js +0 -17
- package/dist/translator/taglib/core/macro/translate.js +0 -48
- package/dist/translator/taglib/core/parse-class.js +0 -79
- package/dist/translator/taglib/core/parse-export.js +0 -14
- package/dist/translator/taglib/core/parse-import.js +0 -14
- package/dist/translator/taglib/core/parse-module-code.js +0 -18
- package/dist/translator/taglib/core/parse-static.js +0 -18
- package/dist/translator/taglib/core/transform-style.js +0 -66
- package/dist/translator/taglib/core/translate-await.js +0 -41
- package/dist/translator/taglib/core/translate-body.js +0 -17
- package/dist/translator/taglib/core/translate-for.js +0 -156
- package/dist/translator/taglib/core/translate-html-comment.js +0 -52
- package/dist/translator/taglib/core/translate-include-content.js +0 -53
- package/dist/translator/taglib/core/translate-server-only.js +0 -5
- package/dist/translator/taglib/core/translate-while.js +0 -32
- package/dist/translator/taglib/index.js +0 -6
- package/dist/translator/taglib/migrate/all-templates.js +0 -46
- package/dist/translator/taglib/migrate/index.js +0 -5
- package/dist/translator/text/index.js +0 -10
- package/dist/translator/text/index[html].js +0 -12
- package/dist/translator/text/index[vdom].js +0 -20
- package/dist/translator/util/add-dependencies.js +0 -329
- package/dist/translator/util/escape-regexp.js +0 -4
- package/dist/translator/util/get-component-files.js +0 -86
- package/dist/translator/util/html-out-write.js +0 -15
- package/dist/translator/util/key-manager.js +0 -176
- package/dist/translator/util/optimize-html-writes.js +0 -52
- package/dist/translator/util/optimize-vdom-create.js +0 -164
- package/dist/translator/util/plugin-hooks.js +0 -22
- package/dist/translator/util/runtime-flags.js +0 -3
- package/dist/translator/util/vdom-out-write.js +0 -10
- package/dist/translator/util/with-previous-location.js +0 -6
- package/docs/10-awesome-marko-features.md +0 -291
- package/docs/body-content.md +0 -355
- package/docs/class-components.md +0 -1001
- package/docs/cloudflare-workers.md +0 -32
- package/docs/compiler-hooks.png +0 -0
- package/docs/compiler.md +0 -473
- package/docs/component-diagram.afdesign +0 -0
- package/docs/component-diagram.svg +0 -87
- package/docs/concise.md +0 -141
- package/docs/conditionals-and-lists.md +0 -72
- package/docs/core-tags.md +0 -325
- package/docs/custom-tags.md +0 -201
- package/docs/editor-plugins.md +0 -45
- package/docs/events.md +0 -131
- package/docs/express.md +0 -66
- package/docs/fastify.md +0 -65
- package/docs/getting-started.md +0 -95
- package/docs/http.md +0 -34
- package/docs/icons/js.svg +0 -4
- package/docs/icons/marko.svg +0 -1
- package/docs/icons/ts.svg +0 -1
- package/docs/installing.md +0 -51
- package/docs/koa.md +0 -35
- package/docs/lasso.md +0 -196
- package/docs/marko-5-upgrade.md +0 -59
- package/docs/marko-json.md +0 -248
- package/docs/marko-vs-react.md +0 -854
- package/docs/redux.md +0 -61
- package/docs/rendering.md +0 -268
- package/docs/rollup.md +0 -352
- package/docs/state.md +0 -105
- package/docs/structure.json +0 -49
- package/docs/styles.md +0 -29
- package/docs/syntax.md +0 -601
- package/docs/troubleshooting-streaming.md +0 -68
- package/docs/typescript.md +0 -406
- package/docs/vite.md +0 -86
- package/docs/webpack.md +0 -205
- package/docs/why-is-marko-fast.md +0 -239
- package/env.js +0 -7
- package/helpers/README.md +0 -3
- package/helpers/empty.js +0 -4
- package/helpers/notEmpty.js +0 -11
- package/index-browser.marko +0 -15
- package/index.d.ts +0 -375
- package/index.js +0 -7
- package/legacy-components-browser.marko +0 -15
- package/legacy-components.js +0 -7
- package/node-require.js +0 -8
- package/src/build.json +0 -3
- package/src/compiler/config.js +0 -45
- package/src/compiler/index.js +0 -164
- package/src/compiler/modules.js +0 -5
- package/src/core-tags/.eslintrc +0 -5
- package/src/core-tags/components/init-components-tag.js +0 -54
- package/src/core-tags/components/preferred-script-location-tag.js +0 -25
- package/src/core-tags/components/preserve-tag.js +0 -1
- package/src/core-tags/core/__flush_here_and_after__.js +0 -44
- package/src/core-tags/core/await/AsyncValue.js +0 -121
- package/src/core-tags/core/await/client-reorder-runtime.js +0 -51
- package/src/core-tags/core/await/index.d.marko +0 -10
- package/src/core-tags/core/await/renderer.js +0 -249
- package/src/core-tags/core/await/reorderer-renderer.js +0 -156
- package/src/index.js +0 -4
- package/src/node-require/browser-refresh.js +0 -18
- package/src/node-require/hot-reload.js +0 -45
- package/src/node-require/index.js +0 -103
- package/src/node_modules/@internal/components-beginComponent/index-browser.js +0 -23
- package/src/node_modules/@internal/components-beginComponent/index.js +0 -84
- package/src/node_modules/@internal/components-beginComponent/package.json +0 -11
- package/src/node_modules/@internal/components-define-widget-legacy/index-browser.js +0 -335
- package/src/node_modules/@internal/components-define-widget-legacy/index.js +0 -18
- package/src/node_modules/@internal/components-define-widget-legacy/package.json +0 -11
- package/src/node_modules/@internal/components-endComponent/index-browser.js +0 -5
- package/src/node_modules/@internal/components-endComponent/index.js +0 -11
- package/src/node_modules/@internal/components-endComponent/package.json +0 -11
- package/src/node_modules/@internal/components-entry/index-browser.js +0 -11
- package/src/node_modules/@internal/components-entry/index.js +0 -301
- package/src/node_modules/@internal/components-entry/package.json +0 -11
- package/src/node_modules/@internal/components-entry-legacy/index-browser.js +0 -76
- package/src/node_modules/@internal/components-entry-legacy/index.js +0 -16
- package/src/node_modules/@internal/components-entry-legacy/package.json +0 -11
- package/src/node_modules/@internal/components-registry/index-browser.js +0 -586
- package/src/node_modules/@internal/components-registry/index.js +0 -44
- package/src/node_modules/@internal/components-registry/package.json +0 -11
- package/src/node_modules/@internal/components-util/index-browser.js +0 -152
- package/src/node_modules/@internal/components-util/index.js +0 -75
- package/src/node_modules/@internal/components-util/package.json +0 -11
- package/src/node_modules/@internal/create-readable/index-browser.js +0 -30
- package/src/node_modules/@internal/create-readable/index.js +0 -57
- package/src/node_modules/@internal/create-readable/package.json +0 -11
- package/src/node_modules/@internal/loader/fallback-node.js +0 -89
- package/src/node_modules/@internal/loader/index-browser.js +0 -2
- package/src/node_modules/@internal/loader/index.js +0 -23
- package/src/node_modules/@internal/loader/package.json +0 -10
- package/src/node_modules/@internal/preserve-tag/index-browser.js +0 -46
- package/src/node_modules/@internal/preserve-tag/index.js +0 -34
- package/src/node_modules/@internal/preserve-tag/package.json +0 -11
- package/src/node_modules/@internal/require/index-browser.js +0 -15
- package/src/node_modules/@internal/require/index-legacy-browser.js +0 -20
- package/src/node_modules/@internal/require/index-webpack.js +0 -16
- package/src/node_modules/@internal/require/index.js +0 -15
- package/src/node_modules/@internal/require/package.json +0 -11
- package/src/node_modules/@internal/set-immediate/index-browser.js +0 -19
- package/src/node_modules/@internal/set-immediate/index-worker.js +0 -31
- package/src/node_modules/@internal/set-immediate/index.js +0 -3
- package/src/node_modules/@internal/set-immediate/package.json +0 -11
- package/src/node_modules/@internal/set-immediate/queueMicrotask.js +0 -8
- package/src/runtime/.eslintrc +0 -5
- package/src/runtime/RenderResult.js +0 -112
- package/src/runtime/components/Component.js +0 -663
- package/src/runtime/components/ComponentDef.js +0 -169
- package/src/runtime/components/ComponentsContext.js +0 -59
- package/src/runtime/components/GlobalComponentsContext.js +0 -10
- package/src/runtime/components/KeySequence.js +0 -16
- package/src/runtime/components/ServerComponent.js +0 -73
- package/src/runtime/components/State.js +0 -101
- package/src/runtime/components/attach-detach.js +0 -60
- package/src/runtime/components/defineComponent.js +0 -60
- package/src/runtime/components/dom-data.js +0 -8
- package/src/runtime/components/event-delegation.js +0 -144
- package/src/runtime/components/index.js +0 -1
- package/src/runtime/components/legacy/browser.json +0 -9
- package/src/runtime/components/legacy/defineComponent-legacy.js +0 -27
- package/src/runtime/components/legacy/defineRenderer-legacy.js +0 -171
- package/src/runtime/components/legacy/defineWidget-legacy.js +0 -1
- package/src/runtime/components/legacy/dependencies/html.js +0 -3
- package/src/runtime/components/legacy/dependencies/index.js +0 -177
- package/src/runtime/components/legacy/dependencies/vdom.js +0 -3
- package/src/runtime/components/legacy/index.js +0 -1
- package/src/runtime/components/legacy/jquery.js +0 -52
- package/src/runtime/components/legacy/ready.js +0 -152
- package/src/runtime/components/legacy/renderer-legacy.js +0 -231
- package/src/runtime/components/registry.js +0 -1
- package/src/runtime/components/renderer.js +0 -236
- package/src/runtime/components/update-manager.js +0 -94
- package/src/runtime/createOut.js +0 -13
- package/src/runtime/dom-insert.js +0 -77
- package/src/runtime/events.js +0 -2
- package/src/runtime/helpers/_change-case.js +0 -45
- package/src/runtime/helpers/assign.js +0 -21
- package/src/runtime/helpers/attr-tag.js +0 -43
- package/src/runtime/helpers/class-value.js +0 -30
- package/src/runtime/helpers/dynamic-tag.js +0 -181
- package/src/runtime/helpers/empty-component.js +0 -1
- package/src/runtime/helpers/merge.js +0 -13
- package/src/runtime/helpers/of-fallback.js +0 -4
- package/src/runtime/helpers/render-tag.js +0 -20
- package/src/runtime/helpers/serialize-noop.js +0 -5
- package/src/runtime/helpers/style-value.js +0 -45
- package/src/runtime/helpers/tags-compat/dom-debug.js +0 -1
- package/src/runtime/helpers/tags-compat/dom-debug.mjs +0 -4
- package/src/runtime/helpers/tags-compat/dom.js +0 -1
- package/src/runtime/helpers/tags-compat/dom.mjs +0 -4
- package/src/runtime/helpers/tags-compat/html-debug.js +0 -3
- package/src/runtime/helpers/tags-compat/html-debug.mjs +0 -4
- package/src/runtime/helpers/tags-compat/html.js +0 -3
- package/src/runtime/helpers/tags-compat/html.mjs +0 -4
- package/src/runtime/helpers/tags-compat/runtime-dom.js +0 -239
- package/src/runtime/helpers/tags-compat/runtime-html.js +0 -136
- package/src/runtime/helpers/to-string.js +0 -5
- package/src/runtime/html/AsyncStream.js +0 -805
- package/src/runtime/html/BufferedWriter.js +0 -52
- package/src/runtime/html/StringWriter.js +0 -73
- package/src/runtime/html/helpers/_dynamic-attr.js +0 -35
- package/src/runtime/html/helpers/attr.js +0 -80
- package/src/runtime/html/helpers/attrs.js +0 -26
- package/src/runtime/html/helpers/class-attr.js +0 -8
- package/src/runtime/html/helpers/data-marko.js +0 -36
- package/src/runtime/html/helpers/escape-quotes.js +0 -35
- package/src/runtime/html/helpers/escape-script-placeholder.js +0 -24
- package/src/runtime/html/helpers/escape-style-placeholder.js +0 -22
- package/src/runtime/html/helpers/escape-xml.js +0 -21
- package/src/runtime/html/helpers/merge-attrs.js +0 -55
- package/src/runtime/html/helpers/props-script.js +0 -32
- package/src/runtime/html/helpers/style-attr.js +0 -8
- package/src/runtime/html/hot-reload.js +0 -26
- package/src/runtime/html/index.js +0 -33
- package/src/runtime/html/marko-namespace.js +0 -3
- package/src/runtime/renderable.js +0 -194
- package/src/runtime/vdom/AsyncVDOMBuilder.js +0 -456
- package/src/runtime/vdom/VComment.js +0 -24
- package/src/runtime/vdom/VComponent.js +0 -17
- package/src/runtime/vdom/VDocumentFragment.js +0 -34
- package/src/runtime/vdom/VElement.js +0 -375
- package/src/runtime/vdom/VFragment.js +0 -26
- package/src/runtime/vdom/VNode.js +0 -100
- package/src/runtime/vdom/VText.js +0 -25
- package/src/runtime/vdom/helpers/attrs.js +0 -62
- package/src/runtime/vdom/helpers/const-element.js +0 -25
- package/src/runtime/vdom/helpers/merge-attrs.js +0 -16
- package/src/runtime/vdom/hot-reload.js +0 -115
- package/src/runtime/vdom/index.js +0 -30
- package/src/runtime/vdom/is-text-only.js +0 -10
- package/src/runtime/vdom/marko-namespace.js +0 -3
- package/src/runtime/vdom/morphdom/fragment.js +0 -95
- package/src/runtime/vdom/morphdom/helpers.js +0 -42
- package/src/runtime/vdom/morphdom/index.js +0 -741
- package/src/runtime/vdom/parse-html.js +0 -18
- package/src/runtime/vdom/vdom.js +0 -88
- package/src/taglib/index.js +0 -51
- package/src/translator/cdata/index.js +0 -15
- package/src/translator/cdata/index[html].js +0 -15
- package/src/translator/cdata/index[vdom].js +0 -12
- package/src/translator/class.js +0 -65
- package/src/translator/comment/index.js +0 -15
- package/src/translator/comment/index[html].js +0 -17
- package/src/translator/comment/index[vdom].js +0 -3
- package/src/translator/declaration/index.js +0 -15
- package/src/translator/declaration/index[html].js +0 -12
- package/src/translator/declaration/index[vdom].js +0 -3
- package/src/translator/document-type/index.js +0 -15
- package/src/translator/document-type/index[html].js +0 -12
- package/src/translator/document-type/index[vdom].js +0 -3
- package/src/translator/index.js +0 -552
- package/src/translator/placeholder/index.js +0 -15
- package/src/translator/placeholder/index[html].js +0 -93
- package/src/translator/placeholder/index[vdom].js +0 -22
- package/src/translator/scriptlet.js +0 -4
- package/src/translator/tag/attribute/directives/class.js +0 -42
- package/src/translator/tag/attribute/directives/index.js +0 -15
- package/src/translator/tag/attribute/directives/no-update-body-if.js +0 -15
- package/src/translator/tag/attribute/directives/no-update-body.js +0 -10
- package/src/translator/tag/attribute/directives/no-update-if.js +0 -12
- package/src/translator/tag/attribute/directives/no-update.js +0 -48
- package/src/translator/tag/attribute/directives/style.js +0 -42
- package/src/translator/tag/attribute/index.js +0 -150
- package/src/translator/tag/attribute/modifiers/index.js +0 -7
- package/src/translator/tag/attribute/modifiers/no-update.js +0 -14
- package/src/translator/tag/attribute/modifiers/scoped.js +0 -23
- package/src/translator/tag/attribute-tag.js +0 -149
- package/src/translator/tag/custom-tag.js +0 -146
- package/src/translator/tag/dynamic-tag.js +0 -74
- package/src/translator/tag/index.js +0 -287
- package/src/translator/tag/macro-tag.js +0 -7
- package/src/translator/tag/native-tag.js +0 -27
- package/src/translator/tag/native-tag[html]/attributes.js +0 -151
- package/src/translator/tag/native-tag[html]/index.js +0 -221
- package/src/translator/tag/native-tag[vdom]/attributes.js +0 -105
- package/src/translator/tag/native-tag[vdom]/index.js +0 -189
- package/src/translator/tag/util.js +0 -246
- package/src/translator/taglib/core/conditional/translate-else-if.js +0 -20
- package/src/translator/taglib/core/conditional/translate-else.js +0 -20
- package/src/translator/taglib/core/conditional/translate-if.js +0 -8
- package/src/translator/taglib/core/conditional/util.js +0 -41
- package/src/translator/taglib/core/index.js +0 -474
- package/src/translator/taglib/core/macro/parse.js +0 -17
- package/src/translator/taglib/core/macro/translate.js +0 -48
- package/src/translator/taglib/core/parse-class.js +0 -79
- package/src/translator/taglib/core/parse-export.js +0 -14
- package/src/translator/taglib/core/parse-import.js +0 -14
- package/src/translator/taglib/core/parse-module-code.js +0 -18
- package/src/translator/taglib/core/parse-static.js +0 -18
- package/src/translator/taglib/core/transform-style.js +0 -66
- package/src/translator/taglib/core/translate-await.js +0 -41
- package/src/translator/taglib/core/translate-body.js +0 -17
- package/src/translator/taglib/core/translate-for.js +0 -156
- package/src/translator/taglib/core/translate-html-comment.js +0 -52
- package/src/translator/taglib/core/translate-include-content.js +0 -53
- package/src/translator/taglib/core/translate-server-only.js +0 -5
- package/src/translator/taglib/core/translate-while.js +0 -32
- package/src/translator/taglib/index.js +0 -7
- package/src/translator/taglib/migrate/all-templates.js +0 -46
- package/src/translator/taglib/migrate/index.js +0 -5
- package/src/translator/text/index.js +0 -10
- package/src/translator/text/index[html].js +0 -12
- package/src/translator/text/index[vdom].js +0 -20
- package/src/translator/util/add-dependencies.js +0 -329
- package/src/translator/util/escape-regexp.js +0 -4
- package/src/translator/util/get-component-files.js +0 -86
- package/src/translator/util/html-out-write.js +0 -15
- package/src/translator/util/key-manager.js +0 -176
- package/src/translator/util/optimize-html-writes.js +0 -52
- package/src/translator/util/optimize-vdom-create.js +0 -164
- package/src/translator/util/plugin-hooks.js +0 -22
- package/src/translator/util/runtime-flags.js +0 -3
- package/src/translator/util/vdom-out-write.js +0 -10
- package/src/translator/util/with-previous-location.js +0 -6
- package/tags-html.d.ts +0 -3855
- package/translator/index.d.ts +0 -7
- package/translator/package.json +0 -5
package/docs/class-components.md
DELETED
@@ -1,1001 +0,0 @@
|
|
1
|
-
# Class Components
|
2
|
-
|
3
|
-
Marko makes it easy to create user interface components to use as building blocks for web pages and applications of any complexity.
|
4
|
-
|
5
|
-
Marko promotes self-contained components that:
|
6
|
-
|
7
|
-
- Are independently testable
|
8
|
-
- Encapsulate the view, client-side behavior (like event handling) and styling
|
9
|
-
- Can easily be combined to create composite UI components.
|
10
|
-
|
11
|
-
Marko components compile into small, efficient JavaScript modules that hide implementation details from consumers. Components can be published to [npm](https://www.npmjs.com) for reuse across applications.
|
12
|
-
|
13
|
-
## UI component diagram
|
14
|
-
|
15
|
-

|
16
|
-
|
17
|
-
In Marko, the DOM output of a UI component is based on _input properties_ and optional _internal state_ used to control the view.
|
18
|
-
|
19
|
-
If Marko detects changes to `input` or the internal `state`, then the view (that is, the DOM) will automatically update to reflect the new input and state. Internally, Marko uses virtual DOM diffing/patching to update the view, but that’s an implementation detail that could change at any time.
|
20
|
-
|
21
|
-
## Component structure
|
22
|
-
|
23
|
-
Marko makes it easy to keep your component’s class and styles next to the HTML view that they correspond to. The following are the key parts of any UI component:
|
24
|
-
|
25
|
-
- **View** - The HTML template for your UI component. Receives input properties and states, and renders to either server-side HTML or browser-side virtual DOM nodes.
|
26
|
-
- **Client-side behavior** - A JavaScript `class` with methods and properties for initialization, event handling (including DOM events, custom events and lifecycle events), and state management.
|
27
|
-
- **Styles** - Cascading StyleSheets, including support for CSS preprocessors like [Less](https://lesscss.org/) or [Sass](https://sass-lang.com/).
|
28
|
-
|
29
|
-
## Server-side rendering
|
30
|
-
|
31
|
-
A UI component can be rendered on the server or in the browser, but stateful component instances will be automatically mounted to the DOM in the browser for both. If a UI component tree is rendered on the server, then Marko will recreate the UI component tree in the browser with no extra code required. For more details, please see [Rendering](/docs/rendering/).
|
32
|
-
|
33
|
-
## Single-file components
|
34
|
-
|
35
|
-
Marko lets you define a `class` for a component right in the `.marko` file, and call that class’s methods with `on-*` attributes:
|
36
|
-
|
37
|
-
```marko
|
38
|
-
class {
|
39
|
-
onCreate() {
|
40
|
-
this.state = {
|
41
|
-
count: 0
|
42
|
-
};
|
43
|
-
}
|
44
|
-
increment() {
|
45
|
-
this.state.count++;
|
46
|
-
}
|
47
|
-
}
|
48
|
-
|
49
|
-
<label>The current count is <output>${state.count}</output></label>
|
50
|
-
<p><button on-click('increment')>+1</button></p>
|
51
|
-
```
|
52
|
-
|
53
|
-
### Styles
|
54
|
-
|
55
|
-
Adding styles in your view is also made easy:
|
56
|
-
|
57
|
-
```marko
|
58
|
-
style {
|
59
|
-
.primary {
|
60
|
-
background: #09c;
|
61
|
-
}
|
62
|
-
}
|
63
|
-
|
64
|
-
<label>The current count is <output>${state.count}</output></label>
|
65
|
-
<p><button.primary on-click('increment')>+1</button></p>
|
66
|
-
```
|
67
|
-
|
68
|
-
These styles aren’t output in a `<style>` tag as inline styles usually are, but are externalized to deduplicate them across multiple component instances on a page.
|
69
|
-
|
70
|
-
If you use a CSS preprocessor, you can add its file extension on `style`:
|
71
|
-
|
72
|
-
```marko
|
73
|
-
style.less {
|
74
|
-
.primary {
|
75
|
-
background: @primaryColor;
|
76
|
-
}
|
77
|
-
}
|
78
|
-
```
|
79
|
-
|
80
|
-
> **Note:** The code in the `style` section is processed in a context separate from the rest of the template, so you can’t use JavaScript variables inside it. If you need variables in your CSS, use a CSS preprocessor that supports them.
|
81
|
-
|
82
|
-
## Multi-file components
|
83
|
-
|
84
|
-
You might prefer to keep your component’s class and styles in separate files from the view — the classical separation of HTML, CSS, and JavaScript. Marko makes this possible with a filename-based convention.
|
85
|
-
|
86
|
-
> **ProTip:** If your’re moving the component’s class and styles to separate files is because the code is getting too large, consider splitting the component into smaller, more manageable components.
|
87
|
-
|
88
|
-
### Supporting files
|
89
|
-
|
90
|
-
Marko discovers supporting files in the same directory as a Marko view. For example, if you have a view named `counter.marko`, Marko will automatically look for `counter.component.js` and `counter.style.css`.
|
91
|
-
|
92
|
-
```
|
93
|
-
counter.marko
|
94
|
-
counter.component.js
|
95
|
-
counter.style.css
|
96
|
-
```
|
97
|
-
|
98
|
-
Marko also handles views named `index.marko` specially. It will look for `component.js` and `style.css` in addition to `index.component.js` and `index.style.css`. This allows easily grouping component files into a directory:
|
99
|
-
|
100
|
-
```
|
101
|
-
counter/
|
102
|
-
index.marko
|
103
|
-
component.js
|
104
|
-
style.css
|
105
|
-
```
|
106
|
-
|
107
|
-
In your `component.js` file, export the component’s class:
|
108
|
-
|
109
|
-
```js
|
110
|
-
export default class {
|
111
|
-
onCreate() {
|
112
|
-
this.state = {
|
113
|
-
count: 0,
|
114
|
-
};
|
115
|
-
}
|
116
|
-
increment() {
|
117
|
-
this.state.count++;
|
118
|
-
}
|
119
|
-
}
|
120
|
-
```
|
121
|
-
|
122
|
-
In your `index.marko` file, you can reference methods from that class with `on-*` attributes:
|
123
|
-
|
124
|
-
```marko
|
125
|
-
<label>The current count is <output>${state.count}</output></label>
|
126
|
-
<p><button.primary on-click('increment')>+1</button></p>
|
127
|
-
```
|
128
|
-
|
129
|
-
And in your `style.css`, define the styles:
|
130
|
-
|
131
|
-
```css
|
132
|
-
.primary {
|
133
|
-
background: #09c;
|
134
|
-
}
|
135
|
-
```
|
136
|
-
|
137
|
-
> **ProTip:** Marko actually looks any filenames with the pattern `[name].style.*`, so it will pick up any CSS preprocessor file extensions you use: `.less`, `.stylus`, `.scss`, etc.
|
138
|
-
|
139
|
-
### Components with plain objects
|
140
|
-
|
141
|
-
If you target browsers that does not support classes, a plain object of methods can be exported:
|
142
|
-
|
143
|
-
```js
|
144
|
-
export default {
|
145
|
-
onCreate: function () {
|
146
|
-
this.state = {
|
147
|
-
count: 0,
|
148
|
-
};
|
149
|
-
},
|
150
|
-
increment: function () {
|
151
|
-
this.state.count++;
|
152
|
-
},
|
153
|
-
};
|
154
|
-
```
|
155
|
-
|
156
|
-
## Split components
|
157
|
-
|
158
|
-
Split components optimize for when a component renders on the server, and doesn’t need to dynamically rerender in the browser. As a result, its template and logic aren’t sent to the browser, reducing load time and download size.
|
159
|
-
|
160
|
-
> **Note:** If a split component is the child of a stateful component, its full rendering logic will still be sent because the parent may pass new input to the split component and rerender it.
|
161
|
-
|
162
|
-
Additionally, if _all_ components rendered on a page are split components, Marko’s VDOM and rendering runtime is unnecessary, and therefore not sent to the browser.
|
163
|
-
|
164
|
-
> **ProTip:** Don’t over-optimize. If your component really doesn’t need rerendering, go ahead and split, but don’t forgo stateful rerendering when it would make your code more maintainable.
|
165
|
-
|
166
|
-
### Usage
|
167
|
-
|
168
|
-
Marko discovers split components similarly to how it discovers an external component class. For example, if you have a view named `button.marko`, it will automatically look for `button.component-browser.js`. If your view is named `index.marko`, it will look for `component-browser.js` in addition to `index.component-browser.js`.
|
169
|
-
|
170
|
-
```
|
171
|
-
counter/
|
172
|
-
index.marko
|
173
|
-
component-browser.js
|
174
|
-
```
|
175
|
-
|
176
|
-
A split component might need to do some setup as part of its initial render. In this case, the component may define a second component class to use the `onCreate`, `onInput`, and `onRender` [lifecycle methods](#lifecycle-events).
|
177
|
-
|
178
|
-
This class can be exported from `component.js`, or defined right in the template as a single-file components. In this case, your component folder may contain a `component.js` file, and must contain a `component-browser.js`. The following [lifecycle methods](#lifecycle-events) can go inside the `component.js` file:
|
179
|
-
|
180
|
-
```
|
181
|
-
class {
|
182
|
-
onCreate(input, out) { }
|
183
|
-
onInput(input, out) { }
|
184
|
-
onRender(out) { }
|
185
|
-
onDestroy() { }
|
186
|
-
}
|
187
|
-
```
|
188
|
-
|
189
|
-
And the following [lifecycle methods](#lifecycle-events) can go inside the `component-browser.js` file:
|
190
|
-
|
191
|
-
```
|
192
|
-
class {
|
193
|
-
onMount() { }
|
194
|
-
onUpdate() { }
|
195
|
-
}
|
196
|
-
```
|
197
|
-
|
198
|
-
Any JavaScript code related to the DOM or browser should also be inside `component-browser.js`.
|
199
|
-
|
200
|
-
### Example
|
201
|
-
|
202
|
-
`index.marko`
|
203
|
-
|
204
|
-
```marko
|
205
|
-
class {
|
206
|
-
onCreate() {
|
207
|
-
this.number = 123;
|
208
|
-
}
|
209
|
-
}
|
210
|
-
|
211
|
-
<button on-click('shout')>What’s my favorite number?</button>
|
212
|
-
```
|
213
|
-
|
214
|
-
`component-browser.js`
|
215
|
-
|
216
|
-
```js
|
217
|
-
export default {
|
218
|
-
shout() {
|
219
|
-
alert(`My favorite number is ${this.number}!`);
|
220
|
-
},
|
221
|
-
};
|
222
|
-
```
|
223
|
-
|
224
|
-
## Event handling
|
225
|
-
|
226
|
-
The `on-[event](methodName|function, ...args)` attributes allow event listeners to be attached for either:
|
227
|
-
|
228
|
-
- A native DOM event, when used on a native DOM element such as a `<button>`
|
229
|
-
- Or a UI component event, when used on a custom tag for a UI component such as `<my-component>`
|
230
|
-
|
231
|
-
The `on-*` attributes are used to associate event handler methods with an event name. Event handlers may be specified by `'methodName'` — a string that matches a method on the component instance, or they may be a `function`. Attaching listeners for native DOM events and UI component custom events is explained in more detail in the sections below.
|
232
|
-
|
233
|
-
You may also use the `once-[event](methodName|function, ...args)` syntax, which will listen for only the first event, and then remove the listener.
|
234
|
-
|
235
|
-
### Attaching DOM event listeners
|
236
|
-
|
237
|
-
The code below illustrates how to attach an event listener for native DOM events:
|
238
|
-
|
239
|
-
```marko
|
240
|
-
class {
|
241
|
-
onButtonClick(name, event, el) {
|
242
|
-
alert(`Hello ${name}!`);
|
243
|
-
}
|
244
|
-
}
|
245
|
-
|
246
|
-
static function fadeIn(event, el) {
|
247
|
-
el.hidden = false;
|
248
|
-
el.style.opacity = 0;
|
249
|
-
el.style.transition = 'opacity 1s';
|
250
|
-
setTimeout(() => el.style.opacity = 1);
|
251
|
-
}
|
252
|
-
|
253
|
-
<button on-click('onButtonClick', 'Frank')>
|
254
|
-
Say Hello to Frank
|
255
|
-
</button>
|
256
|
-
|
257
|
-
<button on-click('onButtonClick', 'John')>
|
258
|
-
Say Hello to John
|
259
|
-
</button>
|
260
|
-
|
261
|
-
<img src='foo.jpg' once-load(fadeIn) hidden />
|
262
|
-
```
|
263
|
-
|
264
|
-
The following arguments are passed to the event handler when the event occurs:
|
265
|
-
|
266
|
-
1. `...args` - Any extra bound arguments are _prepended_ to the arguments passed to the component’s handler method.
|
267
|
-
For example: `on-click('onButtonClick', arg1, arg2)` → `onButtonClick(arg1, arg2, event, el)`
|
268
|
-
2. `event` - The native DOM event object.
|
269
|
-
3. `el` - The DOM element that the event listener was attached to.
|
270
|
-
|
271
|
-
When using the `on-*` or `once-*` attributes to attach event listeners, Marko uses event delegation that is more efficient than direct attachment of `el.addEventListener()`. Please see [Why is Marko Fast? § Event delegation](/docs/why-is-marko-fast/#event-delegation) for more details.
|
272
|
-
|
273
|
-
<a id="declarative-custom-events"></a>
|
274
|
-
|
275
|
-
### Attaching custom event listeners
|
276
|
-
|
277
|
-
The code below illustrates how to attach an event listener for a UI component’s custom event:
|
278
|
-
|
279
|
-
```marko
|
280
|
-
class {
|
281
|
-
onCounterChange(newValue, el) {
|
282
|
-
alert(`New value: ${newValue}!`);
|
283
|
-
}
|
284
|
-
onCounterMax(max) {
|
285
|
-
alert(`It reached the max: ${max}!`);
|
286
|
-
}
|
287
|
-
}
|
288
|
-
|
289
|
-
<counter on-change('onCounterChange') once-max('onCounterMax') />
|
290
|
-
```
|
291
|
-
|
292
|
-
The following arguments are passed to the event handler when the event occurs:
|
293
|
-
|
294
|
-
1. `...args` - Any extra bound arguments are _prepended_ to the arguments passed to the component’s handler method.
|
295
|
-
2. `...eventArgs` - The arguments passed to `this.emit()` by the target UI component.
|
296
|
-
3. `component` - The component instance that the event listener was attached to.
|
297
|
-
|
298
|
-
The following code illustrates how the UI component for `<counter>` might emit its `change` event:
|
299
|
-
|
300
|
-
`counter/index.marko`
|
301
|
-
|
302
|
-
```marko
|
303
|
-
class {
|
304
|
-
onCreate() {
|
305
|
-
this.max = 50;
|
306
|
-
this.state = { count: 0 };
|
307
|
-
}
|
308
|
-
increment() {
|
309
|
-
if (this.state.count < this.max) {
|
310
|
-
this.emit('change', ++this.state.count);
|
311
|
-
}
|
312
|
-
if (this.state.count === this.max) {
|
313
|
-
this.emit('max', this.state.count);
|
314
|
-
}
|
315
|
-
}
|
316
|
-
}
|
317
|
-
|
318
|
-
|
319
|
-
<button.example-button on-click('increment')>
|
320
|
-
Increment
|
321
|
-
</button>
|
322
|
-
```
|
323
|
-
|
324
|
-
> **ProTip:** Unlike native DOM events, UI component custom events may be emitted with multiple arguments. For example:
|
325
|
-
>
|
326
|
-
> ```js
|
327
|
-
> this.emit("foo", "bar", "baz");
|
328
|
-
> ```
|
329
|
-
|
330
|
-
## Attributes
|
331
|
-
|
332
|
-
### `on-[event](methodName|function, ...args)`
|
333
|
-
|
334
|
-
The `on-*` attribute syntax attaches an event listener to either a native DOM event or a UI component event. The `on-*` attribute associates an event handler method with an event name. Please see the [Event handling](#event-handling) section above for details.
|
335
|
-
|
336
|
-
### `once-[event](methodName|function, ...args)`
|
337
|
-
|
338
|
-
The same as the `on-*` attribut,e except that its listener is only invoked for the first event, and then removed from memory. Please see the [Event handling](#event-handling) section above for more details.
|
339
|
-
|
340
|
-
### `key`
|
341
|
-
|
342
|
-
The `key` property does 2 things in Marko:
|
343
|
-
|
344
|
-
- Obtains references to nested HTML elements and nested UI components.
|
345
|
-
- Matches corresponding elements together when DOM diffing/patching after a rerender. When updating the DOM, keyed elements/components are matched up and reused rather than discarded and recreated.
|
346
|
-
|
347
|
-
Internally, Marko assigns a unique key to all HTML elements and UI components in a `.marko` file, based on the order they appear in the file. If you have repeated elements or elements that move between locations in the DOM, then you likely want to assign a custom `key` by adding a `key` attribute. The `key` attribute can be applied to both HTML elements and custom tags.
|
348
|
-
|
349
|
-
#### Referencing nested HTML elements and components
|
350
|
-
|
351
|
-
```marko
|
352
|
-
class {
|
353
|
-
onMount() {
|
354
|
-
const headerElement = this.getEl('header');
|
355
|
-
const colorListItems = this.getEls('colors');
|
356
|
-
const myFancyButton = this.getComponent('myFancyButton');
|
357
|
-
}
|
358
|
-
}
|
359
|
-
|
360
|
-
<h1 key="header">Hello</h1>
|
361
|
-
|
362
|
-
<ul>
|
363
|
-
<for|color| of=['red', 'green', 'blue']>
|
364
|
-
<li key="colors[]">${color}</li>
|
365
|
-
</for>
|
366
|
-
</ul>
|
367
|
-
|
368
|
-
<fancy-button key="myFancyButton"/>
|
369
|
-
```
|
370
|
-
|
371
|
-
> **Note:** The `[]` suffix (e.g. `key="colors[]"`) lets Marko know that the element will be repeated multiple times with the same key.
|
372
|
-
|
373
|
-
#### Keyed matching
|
374
|
-
|
375
|
-
The `key` attribute can pair an HTML element or UI component that moves to a new location in the DOM. For example:
|
376
|
-
|
377
|
-
```marko
|
378
|
-
class {
|
379
|
-
onCreate() {
|
380
|
-
this.state = {
|
381
|
-
swapped: false
|
382
|
-
}
|
383
|
-
}
|
384
|
-
}
|
385
|
-
|
386
|
-
<if(state.swapped)>
|
387
|
-
<p key="b">B</p>
|
388
|
-
<p key="a">A</p>
|
389
|
-
</if>
|
390
|
-
<else>
|
391
|
-
<p key="a">A</p>
|
392
|
-
<p key="b">B</p>
|
393
|
-
</else>
|
394
|
-
```
|
395
|
-
|
396
|
-
The `key` attribute can be used to pair HTML elements or UI components that are repeated:
|
397
|
-
|
398
|
-
```marko
|
399
|
-
<ul>
|
400
|
-
<for|user| of=input.users>
|
401
|
-
<li key=user.id>${user.name}</li>
|
402
|
-
</for>
|
403
|
-
</ul>
|
404
|
-
```
|
405
|
-
|
406
|
-
This way, if the order of `input.users` changes, the DOM will be rerendered more efficiently.
|
407
|
-
|
408
|
-
#### `*:scoped`
|
409
|
-
|
410
|
-
The `:scoped` attribute modifier results in the attribute value getting prefixed with a unique ID associated with the current UI component. `:scoped` attribute modifiers can be used to assign a globally unique attribute value from a value that only needs to be unique to the current UI component.
|
411
|
-
|
412
|
-
Here’s a use-case: certain HTML attributes reference the `id` of other elements on the page. For example, the [HTML `<label>`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/label) `for` attribute takes an `id` as its value. Many `ARIA` attributes like [`aria-describedby`](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/ARIA_Techniques/Using_the_aria-describedby_attribute) also take an `id` as their value.
|
413
|
-
|
414
|
-
The `:scoped` modifier on an attribute allows you to reference another element without fear of duplicate `id`s, as shown in the following examples:
|
415
|
-
|
416
|
-
**`for:scoped`**
|
417
|
-
|
418
|
-
```marko
|
419
|
-
<label for:scoped="name">Name</label>
|
420
|
-
<input id:scoped="name" value="Frank"/>
|
421
|
-
```
|
422
|
-
|
423
|
-
The above code will output HTML similar to the following:
|
424
|
-
|
425
|
-
```html
|
426
|
-
<label for="c0-name">Name</label> <input id="c0-name" value="Frank" />
|
427
|
-
```
|
428
|
-
|
429
|
-
**`aria-describedby:scoped`**
|
430
|
-
|
431
|
-
```marko
|
432
|
-
<button
|
433
|
-
aria-describedby:scoped="closeDisclaimer"
|
434
|
-
on-click('closeDialog')>Close</button>
|
435
|
-
|
436
|
-
<p id:scoped="closeDisclaimer">
|
437
|
-
Closing this window will discard any entered information and return you to the main page.
|
438
|
-
</p>
|
439
|
-
```
|
440
|
-
|
441
|
-
```html
|
442
|
-
<button aria-describedby="c0-closeDisclaimer">Close</button>
|
443
|
-
|
444
|
-
<p id="c0-closeDisclaimer">
|
445
|
-
Closing this window will discard any entered information and return you to the
|
446
|
-
main page.
|
447
|
-
</p>
|
448
|
-
```
|
449
|
-
|
450
|
-
**`href:scoped`**
|
451
|
-
|
452
|
-
```marko
|
453
|
-
<a href:scoped="#anchor">Jump to section</a>
|
454
|
-
<section id:scoped="anchor"></section>
|
455
|
-
```
|
456
|
-
|
457
|
-
```html
|
458
|
-
<a href="#c0-anchor">Jump to section</a>
|
459
|
-
<section id="c0-anchor"></section>
|
460
|
-
```
|
461
|
-
|
462
|
-
### `no-update`
|
463
|
-
|
464
|
-
Preserves the DOM subtree associated with the element or component, so it won’t be modified when rerendering.
|
465
|
-
|
466
|
-
```marko
|
467
|
-
<!-- Never rerender this table -->
|
468
|
-
<table no-update>
|
469
|
-
…
|
470
|
-
</table>
|
471
|
-
```
|
472
|
-
|
473
|
-
```marko
|
474
|
-
<!-- N ever rerender this UI component -->
|
475
|
-
<app-map no-update/>
|
476
|
-
```
|
477
|
-
|
478
|
-
This is most useful when other JavaScript modifies the DOM tree of an element, like for embeds.
|
479
|
-
|
480
|
-
### `no-update-if`
|
481
|
-
|
482
|
-
Similar to [no-update](#no-update), except that the DOM subtree is _conditionally_ preserved:
|
483
|
-
|
484
|
-
```marko
|
485
|
-
<!-- Don’t re-render this table without table data -->
|
486
|
-
<table no-update-if(input.tableData == null)>
|
487
|
-
…
|
488
|
-
</table>
|
489
|
-
```
|
490
|
-
|
491
|
-
### `no-update-body`
|
492
|
-
|
493
|
-
Similar to [no-update](#no-update), except that only the descendant DOM nodes are preserved:
|
494
|
-
|
495
|
-
```marko
|
496
|
-
<!-- Never rerender any nested DOM elements -->
|
497
|
-
<div no-update-body>
|
498
|
-
…
|
499
|
-
</div>
|
500
|
-
```
|
501
|
-
|
502
|
-
### `no-update-body-if`
|
503
|
-
|
504
|
-
Similar to [no-update-body](#no-update-body), except that its descendant DOM nodes are _conditionally_ preserved:
|
505
|
-
|
506
|
-
```marko
|
507
|
-
<!-- Never rerender any nested DOM elements without table data -->
|
508
|
-
<table no-update-body-if(input.tableData == null)>
|
509
|
-
…
|
510
|
-
</table>
|
511
|
-
```
|
512
|
-
|
513
|
-
### `:no-update`
|
514
|
-
|
515
|
-
Prevents certain attributes from being modified during a rerender. The attribute(s) that should not be modified should have a `:no-update` modifier:
|
516
|
-
|
517
|
-
```marko
|
518
|
-
<!-- Never modify the `class` attribute -->
|
519
|
-
<div class:no-update=input.className>
|
520
|
-
…
|
521
|
-
</div>
|
522
|
-
```
|
523
|
-
|
524
|
-
## Properties
|
525
|
-
|
526
|
-
### `this.id`
|
527
|
-
|
528
|
-
A string identifier for the root HTML element that the component is bound to. (Not the `id` attribute.)
|
529
|
-
|
530
|
-
### `this.state`
|
531
|
-
|
532
|
-
The current state for the component. Changing `this.state` or its direct properties will cause the component to rerender.
|
533
|
-
|
534
|
-
Only properties that exist when `this.state` is first defined will be watched for changes. If you don’t need a property initially, you can set its value to `null`:
|
535
|
-
|
536
|
-
```marko
|
537
|
-
class {
|
538
|
-
onCreate() {
|
539
|
-
this.state = {
|
540
|
-
data: null,
|
541
|
-
error: null
|
542
|
-
}
|
543
|
-
}
|
544
|
-
getData() {
|
545
|
-
fetch('/endpoint')
|
546
|
-
.then(data => this.state.data = data)
|
547
|
-
.catch(error => this.state.error = error);
|
548
|
-
}
|
549
|
-
}
|
550
|
-
```
|
551
|
-
|
552
|
-
Beware: setting a `state` property only _nominates_ the component for a possible rerender, and properties are only watched one level deep. Thus, the component is only rerendered if at least one of the component state properties changed (`oldValue !== newValue`).
|
553
|
-
|
554
|
-
If none of the properties changed (because the new value is identical, or no difference is detected by a shallow comparison), the assignment is considered a no-operation (great for performance).
|
555
|
-
|
556
|
-
We recommend using [immutable data structures](https://wecodetheweb.com/2016/02/12/immutable-javascript-using-es6-and-beyond/), but if you want to mutate a state property (perhaps push a new item into an array), you can mark it as dirty with `setStateDirty`:
|
557
|
-
|
558
|
-
```js
|
559
|
-
this.state.numbers.push(num);
|
560
|
-
|
561
|
-
// Mark numbers as dirty, because a `push`
|
562
|
-
// won’t be automatically detected by Marko
|
563
|
-
this.setStateDirty("numbers");
|
564
|
-
```
|
565
|
-
|
566
|
-
### `this.input`
|
567
|
-
|
568
|
-
The current input for the component. Setting `this.input` will rerender the component which can be useful for testing, but generally avoided (prefer `state` instead).
|
569
|
-
|
570
|
-
## Variables
|
571
|
-
|
572
|
-
When a Marko component is compiled, some additional variables are available to the rendering function. These variables are described below.
|
573
|
-
|
574
|
-
### `component`
|
575
|
-
|
576
|
-
The `component` variable refers to the instance of the currently rendering UI component. This variable can be used to call methods on the UI component instance:
|
577
|
-
|
578
|
-
```marko
|
579
|
-
class {
|
580
|
-
getFullName() {
|
581
|
-
const { person } = this.input;
|
582
|
-
return `${person.firstName} ${person.lastName}`;
|
583
|
-
}
|
584
|
-
}
|
585
|
-
|
586
|
-
<h1>Hello, ${component.getFullName()}</h1>
|
587
|
-
```
|
588
|
-
|
589
|
-
### `input`
|
590
|
-
|
591
|
-
The `input` variable refers to the `input` object, and is equivalent to `component.input`|`this.input`.
|
592
|
-
|
593
|
-
```marko
|
594
|
-
<h1>Hello, ${input.name}</h1>
|
595
|
-
```
|
596
|
-
|
597
|
-
### `state`
|
598
|
-
|
599
|
-
The `state` variable refers to the UI component’s `state` object, and is the _unwatched_ equivalent of `component.state`|`this.state`.
|
600
|
-
|
601
|
-
```marko
|
602
|
-
<h1>Hello ${state.name}</h1>
|
603
|
-
```
|
604
|
-
|
605
|
-
## Methods
|
606
|
-
|
607
|
-
### `destroy([options])`
|
608
|
-
|
609
|
-
| Option | Type | Default | Description |
|
610
|
-
| ------------ | --------- | ------- | --------------------------------------------------------------------------------- |
|
611
|
-
| `removeNode` | `Boolean` | `true` | `false` will keep the component in the DOM while unsubscribing all events from it |
|
612
|
-
| `recursive` | `Boolean` | `true` | `false` will prevent child components from being destroyed |
|
613
|
-
|
614
|
-
Destroys the component by unsubscribing from all listeners made using the `subscribeTo` method, and then detaching the component’s root element from the DOM. All nested components (discovered by querying the DOM) are also destroyed.
|
615
|
-
|
616
|
-
```js
|
617
|
-
component.destroy({
|
618
|
-
removeNode: false, // true by default
|
619
|
-
recursive: false, // true by default
|
620
|
-
});
|
621
|
-
```
|
622
|
-
|
623
|
-
### `forceUpdate()`
|
624
|
-
|
625
|
-
Queue the component to re-render and skip all checks to see if it actually needs it.
|
626
|
-
|
627
|
-
> When using `forceUpdate()` the updating of the DOM will be queued up. If you want to immediately update the DOM
|
628
|
-
> then call `this.update()` after calling `this.forceUpdate()`.
|
629
|
-
|
630
|
-
### `getEl([key, index])`
|
631
|
-
|
632
|
-
| Signature | Type | Description |
|
633
|
-
| ------------ | ------------- | --------------------------------------------------------------------------------- |
|
634
|
-
| `key` | `String` | _optional_ — the scoped identifier for the element |
|
635
|
-
| `index` | `Number` | _optional_ — the index of the component, if `key` references a repeated component |
|
636
|
-
| return value | `HTMLElement` | The element matching the key, or `this.el` if no key is provided |
|
637
|
-
|
638
|
-
Returns a nested DOM element by prefixing the provided `key` with the component’s ID. For Marko, nested DOM elements should be assigned an ID with the `key` attribute.
|
639
|
-
|
640
|
-
### `getEls(key)`
|
641
|
-
|
642
|
-
| Signature | Type | Description |
|
643
|
-
| ------------ | -------------------- | ----------------------------------------------------- |
|
644
|
-
| `key` | `String` | The scoped identifier for the element |
|
645
|
-
| return value | `Array<HTMLElement>` | An array of _repeated_ DOM elements for the given key |
|
646
|
-
|
647
|
-
Repeated DOM elements must have a value for the `key` attribute that ends with `[]`. For example, `key="items[]"`.
|
648
|
-
|
649
|
-
### `getElId([key, index])`
|
650
|
-
|
651
|
-
| Signature | Type | Description |
|
652
|
-
| ------------ | -------- | --------------------------------------------------------------------------------- |
|
653
|
-
| `key` | `String` | _optional_ — The scoped identifier for the element |
|
654
|
-
| `index` | `Number` | _optional_ — The index of the component, if `key` references a repeated component |
|
655
|
-
| return value | `String` | The element ID matching the key, or `this.el.id` if `key` is undefined |
|
656
|
-
|
657
|
-
Similar to `getEl`, but only returns the String ID of the nested DOM element instead of the actual DOM element.
|
658
|
-
|
659
|
-
### `getComponent(key[, index])`
|
660
|
-
|
661
|
-
| Signature | Type | Description |
|
662
|
-
| ------------ | ----------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
663
|
-
| `key` | `String` | The scoped identifier for the element |
|
664
|
-
| `index` | `Number` | _optional_ — The index of the component, if `key` references a repeated component |
|
665
|
-
| return value | `Component` | A reference to a nested `Component` for the given key. If an `index` is provided and the target component is a repeated component (i.e. `key="items[]"`), then the component at the given index will be returned. |
|
666
|
-
|
667
|
-
For example, given the following component,
|
668
|
-
|
669
|
-
```marko
|
670
|
-
<app-main>
|
671
|
-
<app-child key="child"/>
|
672
|
-
</app-main>
|
673
|
-
```
|
674
|
-
|
675
|
-
The following code can be used to get the `<app-child/>` component:
|
676
|
-
|
677
|
-
```js
|
678
|
-
const childComponent = this.getComponent("child");
|
679
|
-
```
|
680
|
-
|
681
|
-
### `getComponents(key, [, index])`
|
682
|
-
|
683
|
-
| Signature | Type | Description |
|
684
|
-
| ------------ | ------------------ | --------------------------------------------------------------------------------- |
|
685
|
-
| `key` | `String` | The scoped identifier for the element |
|
686
|
-
| `index` | `Number` | _optional_ — The index of the component, if `key` references a repeated component |
|
687
|
-
| return value | `Array<Component>` | An array of _repeated_ `Component` instances for the given key |
|
688
|
-
|
689
|
-
Repeated components must have a value for the `key` attribute that ends with `[]`, like `key="items[]"`.
|
690
|
-
|
691
|
-
### `isDestroyed()`
|
692
|
-
|
693
|
-
Returns `true` if a component has been destroyed using [`component.destroy()`](#ondestroy), otherwise `false`.
|
694
|
-
|
695
|
-
### `isDirty()`
|
696
|
-
|
697
|
-
Returns `true` if the component needs a bath.
|
698
|
-
|
699
|
-
### `replaceState(newState)`
|
700
|
-
|
701
|
-
| Signature | Type | Description |
|
702
|
-
| ---------- | -------- | ------------------------------------------------ |
|
703
|
-
| `newState` | `Object` | A new state object to replace the previous state |
|
704
|
-
|
705
|
-
Replaces the state with an entirely new state. Equivalent to `this.state = newState`.
|
706
|
-
|
707
|
-
> **Note:** While `setState()` is additive and will not remove properties that are in the old state but not in the new state, `replaceState()` _will_ add the new state and remove the old state properties that are not found in the new state. Thus, if `replaceState()` is used, consider possible side effects if the new state contains less or other properties than the replaced state.
|
708
|
-
|
709
|
-
### `rerender([input])`
|
710
|
-
|
711
|
-
| Signature | Type | Description |
|
712
|
-
| --------- | -------- | --------------------------------------------------- |
|
713
|
-
| `input` | `Object` | _optional_ — New input data to use when rerendering |
|
714
|
-
|
715
|
-
Rerenders the component using its `renderer`, and either supplied `input` or internal `input` and `state`.
|
716
|
-
|
717
|
-
### `setState(name, value)`
|
718
|
-
|
719
|
-
| Signature | Type | Description |
|
720
|
-
| --------- | -------- | ------------------------------------------ |
|
721
|
-
| `name` | `String` | The name of the `state` property to update |
|
722
|
-
| `value` | `Any` | The new value for the `state` property |
|
723
|
-
|
724
|
-
Changes the value of a single `state` property. Equivalent to `this.state[name] = value`, except it will also work for adding new properties to the component state.
|
725
|
-
|
726
|
-
```js
|
727
|
-
this.setState("disabled", true);
|
728
|
-
```
|
729
|
-
|
730
|
-
### `setState(newState)`
|
731
|
-
|
732
|
-
| Signature | Type | Description |
|
733
|
-
| ---------- | -------- | --------------------------------------------------- |
|
734
|
-
| `newState` | `Object` | A new state object to merge into the previous state |
|
735
|
-
|
736
|
-
Changes the value of multiple state properties:
|
737
|
-
|
738
|
-
```js
|
739
|
-
this.setState({
|
740
|
-
disabled: true,
|
741
|
-
size: "large",
|
742
|
-
});
|
743
|
-
```
|
744
|
-
|
745
|
-
### `setStateDirty(name[, value])`
|
746
|
-
|
747
|
-
| Signature | Type | Description |
|
748
|
-
| --------- | -------- | ------------------------------------------------- |
|
749
|
-
| `name` | `String` | The name of the `state` property to mark as dirty |
|
750
|
-
| `value` | `Any` | _optional_ — A new value for the `state` property |
|
751
|
-
|
752
|
-
Forces a state property change, even if the value is equal to the old value. This is helpful in cases where a change occurs to a complex object that would not be detected by a shallow compare. Invoking this function completely circumvents all property equality checks (shallow compares) and always rerenders the component.
|
753
|
-
|
754
|
-
#### More details
|
755
|
-
|
756
|
-
The first parameter, `name`, is used to allow update handlers (e.g. `update_foo(newValue)`) to handle the state transition for the specific state property that was marked dirty.
|
757
|
-
|
758
|
-
The second parameter, `value`, is used as the new value that is given to update handlers. Because `setStateDirty()` always bypasses all property equality checks, this parameter is optional. If not given or equal to the old value, the old value will be used for the update handler.
|
759
|
-
|
760
|
-
Important: the given parameters do not affect how or if `setStateDirty()` rerenders a component; they are only considered as additional information to update handlers.
|
761
|
-
|
762
|
-
```js
|
763
|
-
// Because this does not create a new array, the change
|
764
|
-
// would not be detected by a shallow property comparison
|
765
|
-
this.state.colors.push("red");
|
766
|
-
|
767
|
-
// Force that particular state property to be considered dirty so
|
768
|
-
// that it will trigger the component's view to be updated
|
769
|
-
this.setStateDirty("colors");
|
770
|
-
```
|
771
|
-
|
772
|
-
### `subscribeTo(emitter)`
|
773
|
-
|
774
|
-
| Signature | Description |
|
775
|
-
| ------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------- |
|
776
|
-
| `emitter` | A [Node.js `EventEmitter`](https://nodejs.org/api/events.html#events_class_eventemitter) or DOM object that emits events (`window`, `document`, etc.) |
|
777
|
-
| return value | A tracked subscription |
|
778
|
-
|
779
|
-
When a component is destroyed, it is necessary to remove any listeners that were attached by the component to prevent memory leaks. By using `subscribeTo`, Marko will automatically track and remove any listeners you attach when the component is destroyed.
|
780
|
-
|
781
|
-
Marko uses [`listener-tracker`](https://github.com/patrick-steele-idem/listener-tracker) to provide this feature.
|
782
|
-
|
783
|
-
```js
|
784
|
-
this.subscribeTo(window).on("scroll", () =>
|
785
|
-
console.log("The user scrolled the window!"),
|
786
|
-
);
|
787
|
-
```
|
788
|
-
|
789
|
-
### `update()`
|
790
|
-
|
791
|
-
Immediately executes any pending updates to the DOM, rather than following the normal queued update mechanism for rendering.
|
792
|
-
|
793
|
-
```js
|
794
|
-
this.setState("foo", "bar");
|
795
|
-
this.update(); // Force the DOM to update
|
796
|
-
this.setState("hello", "world");
|
797
|
-
this.update(); // Force the DOM to update
|
798
|
-
```
|
799
|
-
|
800
|
-
## Event methods
|
801
|
-
|
802
|
-
Marko components inherit from [`EventEmitter`](https://nodejs.org/api/events.html#events_class_eventemitter). Below are a few commonly used methods — view the Node.js docs for the full list.
|
803
|
-
|
804
|
-
### `emit(eventName, ...args)`
|
805
|
-
|
806
|
-
| Signature | Type | Description |
|
807
|
-
| ----------- | -------- | ----------------------------------------------------- |
|
808
|
-
| `eventName` | `String` | Name of the event |
|
809
|
-
| `...args` | `Any` | All subsequent parameters are passed to the listeners |
|
810
|
-
|
811
|
-
Emits a UI component custom event. If a UI component attached a listener with the matching `eventName`, then the corresponding event listener method will be invoked. Event listeners can be attached using either the [`on-[event](methodName|function, ...args)`](#declarative-custom-events) attribute syntax, or `targetComponent.on()`.
|
812
|
-
|
813
|
-
### `on(eventName, handler)`
|
814
|
-
|
815
|
-
| Signature | Type | Description |
|
816
|
-
| ----------- | ---------- | ----------------------------------------- |
|
817
|
-
| `eventName` | `String` | Name of the event to listen for |
|
818
|
-
| `handler` | `Function` | The function to call when the event fires |
|
819
|
-
|
820
|
-
Adds the listener function to the end of the listeners array for the `eventName` event. Does not check to see if the listener has already been added. Multiple calls passing the same combination of `eventName` and `handler` will result in the listener being added and called multiple times.
|
821
|
-
|
822
|
-
### `once(eventName, handler)`
|
823
|
-
|
824
|
-
| Signature | Type | Description |
|
825
|
-
| ----------- | ---------- | ------------------------------------------ |
|
826
|
-
| `eventName` | `String` | Name of the event to listen for |
|
827
|
-
| `handler` | `Function` | Tthe function to call when the event fires |
|
828
|
-
|
829
|
-
Adds a one-time listener function for the `eventName` event. The next time `eventName` triggers, this listener is removed and then invoked.
|
830
|
-
|
831
|
-
## Lifecycle events
|
832
|
-
|
833
|
-
Marko defines six lifecycle events:
|
834
|
-
|
835
|
-
- `create`
|
836
|
-
- `input`
|
837
|
-
- `render`
|
838
|
-
- `mount`
|
839
|
-
- `update`
|
840
|
-
- `destroy`
|
841
|
-
|
842
|
-
These events are emitted at specific points over the lifecycle of a component, as shown below:
|
843
|
-
|
844
|
-
**First render**
|
845
|
-
|
846
|
-
```js
|
847
|
-
emit('create') → emit('input') → emit('render') → emit('mount')
|
848
|
-
```
|
849
|
-
|
850
|
-
**New input**
|
851
|
-
|
852
|
-
```js
|
853
|
-
emit('input') → emit('render') → emit('update')
|
854
|
-
```
|
855
|
-
|
856
|
-
**Internal state change**
|
857
|
-
|
858
|
-
```js
|
859
|
-
emit('render') → emit('update')
|
860
|
-
```
|
861
|
-
|
862
|
-
**Destroy**
|
863
|
-
|
864
|
-
```js
|
865
|
-
emit("destroy");
|
866
|
-
```
|
867
|
-
|
868
|
-
### Lifecycle event methods
|
869
|
-
|
870
|
-
Each lifecycle event has a corresponding component lifecycle method that can listen for the event:
|
871
|
-
|
872
|
-
```js
|
873
|
-
class {
|
874
|
-
onCreate(input, out) { }
|
875
|
-
onInput(input, out) { }
|
876
|
-
onRender(out) { }
|
877
|
-
onMount() { }
|
878
|
-
onUpdate() { }
|
879
|
-
onDestroy() { }
|
880
|
-
}
|
881
|
-
```
|
882
|
-
|
883
|
-
> **ProTip:** When a lifecycle event occurs in the browser, the corresponding event is emitted on the component instance. A parent component, or other code that has access to the component instance, can listen for these events. For example:
|
884
|
-
>
|
885
|
-
> ```js
|
886
|
-
> component.on("input", function (input, out) {
|
887
|
-
> // The component received an input
|
888
|
-
> });
|
889
|
-
> ```
|
890
|
-
|
891
|
-
### `onCreate(input, out)`
|
892
|
-
|
893
|
-
| Signature | Description |
|
894
|
-
| --------- | --------------------------------------------------------------- |
|
895
|
-
| `input` | The input data used to render the component for the first time |
|
896
|
-
| `out` | The async `out` used to render the component for the first time |
|
897
|
-
|
898
|
-
The `create` event is emitted (and `onCreate` is called) when the component is first created.
|
899
|
-
|
900
|
-
`onCreate` is typically used to set the initial state for stateful components:
|
901
|
-
|
902
|
-
```marko
|
903
|
-
class {
|
904
|
-
onCreate(input) {
|
905
|
-
this.state = { count: input.initialCount };
|
906
|
-
}
|
907
|
-
}
|
908
|
-
```
|
909
|
-
|
910
|
-
### `onInput(input, out)`
|
911
|
-
|
912
|
-
| Signature | Description |
|
913
|
-
| --------- | ------------------ |
|
914
|
-
| `input` | The new input data |
|
915
|
-
|
916
|
-
The `input` event is emitted (and `onInput` is called) when the component receives input: both the initial input, and for any subsequent updates to its input.
|
917
|
-
|
918
|
-
### `onRender(out)`
|
919
|
-
|
920
|
-
| Signature | Description |
|
921
|
-
| --------- | -------------------------------------- |
|
922
|
-
| `out` | The async `out` for the current render |
|
923
|
-
|
924
|
-
The `render` event is emitted (and `onRender` is called) when the component is about to render or rerender.
|
925
|
-
|
926
|
-
### `onMount()`
|
927
|
-
|
928
|
-
The `mount` event is emitted (and `onMount` is called) when the component is first mounted to the DOM. For server-rendered components, this is the first event that is emitted only in the browser.
|
929
|
-
|
930
|
-
This is the first point at which `this.el` and `this.els` are defined. `onMount` is commonly used to attach third-party JavaScript to the newly-mounted DOM.
|
931
|
-
|
932
|
-
For example, attaching a library that monitors if the component is in the viewport:
|
933
|
-
|
934
|
-
```marko
|
935
|
-
import scrollmonitor from 'scrollmonitor';
|
936
|
-
|
937
|
-
class {
|
938
|
-
onMount() {
|
939
|
-
this.watcher = scrollmonitor.create(this.el);
|
940
|
-
this.watcher.enterViewport(() => console.log('I have entered the viewport'));
|
941
|
-
this.watcher.exitViewport(() => console.log('I have left the viewport'));
|
942
|
-
}
|
943
|
-
}
|
944
|
-
```
|
945
|
-
|
946
|
-
### `onUpdate()`
|
947
|
-
|
948
|
-
The `update` event is emitted (and `onUpdate` is called) when the component is called after a component rerenders and the DOM has been updated. If a rerender does not update the DOM (nothing changed), this event will not fire.
|
949
|
-
|
950
|
-
### `onDestroy()`
|
951
|
-
|
952
|
-
The `destroy` event is emitted (and `onDestroy` is called) when the component is about to unmount from the DOM and cleaned up. `onDestroy` should be used to do any additional cleanup beyond what Marko handles itself.
|
953
|
-
|
954
|
-
For example, cleaning up from our `scrollmonitor` example in [`onMount`](#onmount):
|
955
|
-
|
956
|
-
```marko
|
957
|
-
import scrollmonitor from 'scrollmonitor';
|
958
|
-
|
959
|
-
class {
|
960
|
-
onMount() {
|
961
|
-
this.watcher = scrollmonitor.create(this.el);
|
962
|
-
this.watcher.enterViewport(() => console.log('Entered the viewport'));
|
963
|
-
this.watcher.exitViewport(() => console.log('Left the viewport'));
|
964
|
-
}
|
965
|
-
onDestroy() {
|
966
|
-
this.watcher.destroy();
|
967
|
-
}
|
968
|
-
}
|
969
|
-
```
|
970
|
-
|
971
|
-
## DOM manipulation methods
|
972
|
-
|
973
|
-
The following methods move the component’s root DOM node(s) from the current parent element to a new parent element (or out of the DOM in the case of `detach`).
|
974
|
-
|
975
|
-
### `appendTo(targetEl)`
|
976
|
-
|
977
|
-
Moves the UI component’s DOM elements into the position after the target element’s last child.
|
978
|
-
|
979
|
-
```js
|
980
|
-
this.appendTo(document.body);
|
981
|
-
```
|
982
|
-
|
983
|
-
### `insertAfter(targetEl)`
|
984
|
-
|
985
|
-
Moves the UI component’s DOM elements into the position after the target DOM element.
|
986
|
-
|
987
|
-
### `insertBefore(targetEl)`
|
988
|
-
|
989
|
-
Moves the UI component’s DOM elements into the position before the target DOM element.
|
990
|
-
|
991
|
-
### `prependTo(targetEl)`
|
992
|
-
|
993
|
-
Moves the UI component’s DOM elements into the position before the target element’s first child.
|
994
|
-
|
995
|
-
### `replace(targetEl)`
|
996
|
-
|
997
|
-
Replaces the target element with the UI component’s DOM elements.
|
998
|
-
|
999
|
-
### `replaceChildrenOf(targetEl)`
|
1000
|
-
|
1001
|
-
Replaces the target element’s children with the UI component’s DOM elements.
|