diff --git a/docs/developer/index.md b/docs/developer/index.md new file mode 100644 index 000000000..6396eaa21 --- /dev/null +++ b/docs/developer/index.md @@ -0,0 +1,6 @@ +# Developer documentation + +- [Patternslib overview](overview.md) +- [The structure of a pattern](patterns.md) +- [Configuring a pattern](patterns-configuration.md) +- [Styleguide](styleguide.md) diff --git a/docs/developer/patterns.rst b/docs/developer/patterns.rst index 9be7d1e47..5edf115a6 100644 --- a/docs/developer/patterns.rst +++ b/docs/developer/patterns.rst @@ -1,20 +1,46 @@ -Creating a new pattern -====================== +The structure of a pattern +========================== -Patterns are implemented as JavaScript classes that are registered with the patterns library. -Below is a minimal skeleton for a pattern. +Patterns are implemented as JavaScript classes that are registered with the patterns registry. +Below is a minimalistic skeleton for a pattern with explanations as inline comments. .. code-block:: javascript :linenos: import { BasePattern } from "@patternslib/patternslib/src/core/basepattern"; + import Parser from "@patternslib/patternslib/src/core/parser"; import registry from "@patternslib/patternslib/src/core/registry"; + export const parser = new Parser("test-pattern"); + // Define an argument with a default value. You can configure the value via + // data-attributes. + parser.addArgument("example-option", "Hollareidulio"); + class Pattern extends BasePattern { + + // Define a name for the pattern which is used as key in the pattern + // registry. static name = "test-pattern"; + + // Define a CSS selector. The pattern will be initialized on elements + // matching this selector. static trigger = ".pat-test-pattern"; + // The parser instance from above. + static parser = parser; + init() { + import("./test-pattern.scss"); + + // Try to avoid jQuery, but here is how to import it, asynchronously. + // eslint-disable-next-line no-unused-vars + const $ = (await import("jquery")).default; + + // The options are automatically created, if parser is defined. + const example_option = this.options.exampleOption; + this.el.innerHTML = ` +
${example_option}, this is the ${this.name} pattern!
+ `; } } @@ -29,55 +55,12 @@ Below is a minimal skeleton for a pattern. export { Pattern }; -This skeleton does several things: - -* lines 1-4 use `RequireJShello, ${example_option}, this is pattern ${this.name} speaking.
+${example_option}, this is the ${this.name} pattern!
`; } }