Skip to content

Commit cda0871

Browse files
authored
Merge branch 'master' into users/janechu/add-frontpage-to-docs
2 parents 3a44f0d + 0e3632f commit cda0871

File tree

1 file changed

+145
-1
lines changed

1 file changed

+145
-1
lines changed

sites/website/src/docs/migration-guide.md

Lines changed: 145 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,148 @@ keywords:
88
- web components
99
---
1010

11-
// TODO
11+
# Migrating from 1.x to 2.x
12+
13+
## Breaking Changes
14+
15+
### `cssPartial`
16+
17+
The `cssPartial` export no longer exists, in its place use `css.partial`.
18+
19+
1.x Example:
20+
```ts
21+
import { cssPartial } from "@microsoft/fast-element";
22+
23+
cssPartial`
24+
:host {
25+
color: red;
26+
}`;
27+
```
28+
29+
2.x Example:
30+
```ts
31+
import { css } from "@microsoft/fast-element";
32+
33+
css.partial`
34+
:host {
35+
color: red;
36+
}`;
37+
```
38+
39+
### `CSSDirective`
40+
41+
The `CSSDirective` has been updated to need to be defined. Additionally it cannot be extended from and must use `implements`.
42+
43+
1.x Example:
44+
```ts
45+
import { CSSDirective } from "@microsoft/fast-element"
46+
47+
class MyCSSDirective extends CSSDirective {
48+
createCSS() {
49+
return "color: red;"
50+
}
51+
}
52+
53+
export const myCssDirective = new MyCSSDirective();
54+
```
55+
56+
2.x Example:
57+
```ts
58+
import { CSSDirective } from "@microsoft/fast-element"
59+
60+
class MyCSSDirective implements CSSDirective {
61+
createCSS() {
62+
return "color: red;"
63+
}
64+
}
65+
66+
CSSDirective.define(MyCSSDirective);
67+
68+
export const myCssDirective = new MyCSSDirective();
69+
```
70+
71+
### `CSSDirective` with behaviors
72+
73+
Behaviors have been extracted from `CSSDirective` so that they can be used in a modular fashion. The API has also been updated to be more intuitive and re-use methods that are common for web components (see `connectedCallback` in the 2.x example below).
74+
75+
1.x Example:
76+
```ts
77+
import { CSSDirective } from "@microsoft/fast-element"
78+
79+
class MyCSSDirective extends CSSDirective {
80+
public cssProperty: string = "background";
81+
82+
createCSS() {
83+
return `display: block; color: red;`
84+
}
85+
86+
createBehavior() {
87+
var that = this;
88+
89+
return {
90+
bind(el) {
91+
el.style.setProperty(that.cssProperty, "yellow")
92+
},
93+
unbind(el) {
94+
el.style.removeProperty(that.cssProperty);
95+
}
96+
}
97+
}
98+
}
99+
100+
export const myCssDirective = new MyCSSDirective();
101+
```
102+
103+
2.x Example:
104+
```ts
105+
import { css, CSSDirective, AddBehavior, HostBehavior } from "@microsoft/fast-element"
106+
107+
const cssProperty: string = "background";
108+
109+
const myCssBehavior: HostBehavior = {
110+
connectedCallback: (controller) => {
111+
controller.addStyles(css`:host {${cssProperty}: yellow }`)
112+
},
113+
disconnectedCallback: (controller) => {
114+
controller.removeStyles(css`:host {${cssProperty}: yellow }`)
115+
}
116+
}
117+
118+
class MyCSSDirective implements CSSDirective {
119+
createCSS(add: AddBehavior) {
120+
add(myCssBehavior);
121+
return `display: block; color: red;`
122+
}
123+
}
124+
125+
CSSDirective.define(MyCSSDirective);
126+
127+
export const myCssDirective = new MyCSSDirective();
128+
```
129+
130+
## Suggested Changes
131+
132+
While some of the APIs remain available, we suggest going forward that certain patterns be updated.
133+
134+
### `@customElement`
135+
136+
We prefer using `FASTElement` and its `.define()` method over the `@customElement` decorator (which is still used under the hood). We believe this more closely matches with how you define custom elements using the native `customElements.define()`.
137+
138+
1.x Example:
139+
```ts
140+
import { FASTElement, customElement } from '@microsoft/fast-element';
141+
142+
@customElement('my-component')
143+
export class MyComponent extends FASTElement {}
144+
```
145+
146+
2.x Example:
147+
```ts
148+
import { FASTElement } from "@microsoft/fast-element";
149+
150+
class MyComponent extends FASTElement {}
151+
152+
MyComponent.define({
153+
name: "my-component"
154+
});
155+
```

0 commit comments

Comments
 (0)