Skip to content

Commit

Permalink
feat(lit): add Context example for Lit (#211)
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyendhst committed Jan 7, 2024
1 parent c642344 commit 247adb3
Show file tree
Hide file tree
Showing 6 changed files with 2,050 additions and 5,059 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,12 @@ How do we solve this ? Developers love having framework overview by examples. It
- [x] Lifecycle
- [x] On mount
- [x] On unmount
- [ ] Component composition
- [x] Component composition
- [x] Props
- [x] Emit to parent
- [x] Slot
- [x] Slot fallback
- [ ] Context
- [x] Context
- [x] Form input
- [x] Input text
- [x] Checkbox
Expand Down
35 changes: 35 additions & 0 deletions content/4-component-composition/5-context/lit/app-x.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { LitElement, html } from "lit";
import { customElement, state } from "lit/decorators.js";
import { ContextProvider } from "@lit/context";

import { userContext } from "./user-context";

import "./user-profile";

@customElement("x-app")
export class XApp extends LitElement {
@state()
user = {
id: 1,
username: "unicorn42",
email: "[email protected]",
};

provider = new ContextProvider(this, {
context: userContext,
initialValue: this.user,
});

updateUsername(newUsername) {
this.user = { ...this.user, username: newUsername };
this.provider.setValue(this.user);
}

render() {
return html` <h1>Welcome back, ${this.user.username}</h1>
<user-profile
.user="${this.user}"
.updateUsername=${this.updateUsername.bind(this)}
></user-profile>`;
}
}
2 changes: 2 additions & 0 deletions content/4-component-composition/5-context/lit/user-context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { createContext } from "@lit/context";
export const userContext = createContext("user");
32 changes: 32 additions & 0 deletions content/4-component-composition/5-context/lit/user-profile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { LitElement, html } from "lit";
import { customElement, property } from "lit/decorators.js";
import { consume } from "@lit/context";

import { userContext } from "./user-context";

@customElement("user-profile")
export class UserProfile extends LitElement {
@consume({ context: userContext })
@property({ type: Object, attribute: false })
user;

@property({ type: Function, attribute: false })
updateUsername;

handleUpdateUsername() {
this.updateUsername("Jane");
}

render() {
return html`
<div>
<h2>My Profile</h2>
<p>Username: ${this.user.username}</p>
<p>Email: ${this.user.email}</p>
<button @click="${this.handleUpdateUsername}">
Update username to Jane
</button>
</div>
`;
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"prepare": "husky install"
},
"dependencies": {
"@lit/context": "^1.1.0",
"@veljs/svelte": "^0.1.11",
"classnames": "^2.5.1",
"eslint-plugin-svelte": "^2.35.1",
Expand Down
Loading

0 comments on commit 247adb3

Please sign in to comment.