-
-
Notifications
You must be signed in to change notification settings - Fork 418
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
Allow lit 3 #2723
Open
alexgagnon
wants to merge
8
commits into
open-wc:master
Choose a base branch
from
alexgagnon:allow-lit-3
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Allow lit 3 #2723
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1f4c338
chore(lit-helpers): bump peerDependencies to also support lit@3
49e1b92
chore(lit-helpers): bump peerDependencies to also support lit@3
1dc6a4f
Merge branch 'master' into allow-lit-3
051b4a7
feat(lit-helpers): support Lit@3
ca15dc0
feat(lit-helpers): support Lit@3
5ffbbca
Merge branch 'allow-lit-3' of github.com:alexgagnon/open-wc into allo…
1bc939c
feat(lit-helpers): support Lit@3
556444f
feat(lit-helpers): support Lit@3
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@open-wc/lit-helpers': minor | ||
--- | ||
|
||
Bump lit-helpers support Lit@3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
spread.js | ||
spread.js.map | ||
spread.d.ts.map | ||
spread.d.ts | ||
spread*.js | ||
spread*.js.map | ||
spread*.d.ts.map | ||
spread*.d.ts | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,253 @@ | ||
import { ElementPart, Part } from 'lit2'; | ||
import { nothing } from 'lit2/html.js'; | ||
import { directive } from 'lit2/directive.js'; | ||
import { AsyncDirective } from 'lit2/async-directive.js'; | ||
|
||
type EventListenerWithOptions = EventListenerOrEventListenerObject & | ||
Partial<AddEventListenerOptions>; | ||
|
||
|
||
/** | ||
* Usage: | ||
* import { html, render } from 'lit'; | ||
* import { spreadProps } from '@open-wc/lit-helpers'; | ||
* | ||
* render( | ||
* html` | ||
* <div | ||
* ${spreadProps({ | ||
* prop1: 'prop1', | ||
* prop2: ['Prop', '2'], | ||
* prop3: { | ||
* prop: 3, | ||
* } | ||
* })} | ||
* ></div> | ||
* `, | ||
* document.body, | ||
* ); | ||
*/ | ||
export class SpreadPropsDirective extends AsyncDirective { | ||
host!: EventTarget | object | Element; | ||
element!: Element; | ||
prevData: { [key: string]: unknown } = {}; | ||
|
||
render(_spreadData: { [key: string]: unknown }) { | ||
return nothing; | ||
} | ||
update(part: Part, [spreadData]: Parameters<this['render']>) { | ||
if (this.element !== (part as ElementPart).element) { | ||
this.element = (part as ElementPart).element; | ||
} | ||
this.host = part.options?.host || this.element; | ||
this.apply(spreadData); | ||
this.groom(spreadData); | ||
this.prevData = { ...spreadData }; | ||
} | ||
|
||
apply(data: { [key: string]: unknown }) { | ||
if (!data) return; | ||
const { prevData, element } = this; | ||
for (const key in data) { | ||
const value = data[key]; | ||
if (value === prevData[key]) { | ||
continue; | ||
} | ||
// @ts-ignore | ||
element[key] = value; | ||
} | ||
} | ||
|
||
groom(data: { [key: string]: unknown }) { | ||
const { prevData, element } = this; | ||
if (!prevData) return; | ||
for (const key in prevData) { | ||
if (!data || (!(key in data) && element[key] === prevData[key])) { | ||
// @ts-ignore | ||
element[key] = undefined; | ||
} | ||
} | ||
} | ||
} | ||
|
||
export const spreadProps = directive(SpreadPropsDirective); | ||
|
||
/** | ||
* Usage: | ||
* import { html, render } from 'lit'; | ||
* import { spreadEvents } from '@open-wc/lit-helpers'; | ||
* | ||
* render( | ||
* html` | ||
* <div | ||
* ${spreadEvents({ | ||
* '@my-event': () => console.log('my-event fired'), | ||
* '@my-other-event': () => console.log('my-other-event fired'), | ||
* '@my-additional-event': | ||
* () => console.log('my-additional-event fired'), | ||
* })} | ||
* ></div> | ||
* `, | ||
* document.body, | ||
* ); | ||
*/ | ||
export class SpreadEventsDirective extends SpreadPropsDirective { | ||
eventData: { [key: string]: unknown } = {}; | ||
|
||
apply(data: { [key: string]: unknown }) { | ||
if (!data) return; | ||
for (const key in data) { | ||
const value = data[key]; | ||
if (value === this.eventData[key]) { | ||
// do nothing if the same value is being applied again. | ||
continue; | ||
} | ||
this.applyEvent(key, value as EventListenerWithOptions); | ||
} | ||
} | ||
|
||
applyEvent(eventName: string, eventValue: EventListenerWithOptions) { | ||
const { prevData, element } = this; | ||
this.eventData[eventName] = eventValue; | ||
const prevHandler = prevData[eventName]; | ||
if (prevHandler) { | ||
element.removeEventListener(eventName, this, eventValue); | ||
} | ||
element.addEventListener(eventName, this, eventValue); | ||
} | ||
|
||
groom(data: { [key: string]: unknown }) { | ||
const { prevData, element } = this; | ||
if (!prevData) return; | ||
for (const key in prevData) { | ||
if (!data || (!(key in data) && element[key] === prevData[key])) { | ||
this.groomEvent(key, prevData[key] as EventListenerWithOptions) | ||
} | ||
} | ||
} | ||
|
||
groomEvent(eventName: string, eventValue: EventListenerWithOptions) { | ||
const { element } = this; | ||
delete this.eventData[eventName]; | ||
element.removeEventListener(eventName, this, eventValue); | ||
} | ||
|
||
handleEvent(event: Event) { | ||
const value: Function | EventListenerObject = this.eventData[ | ||
event.type | ||
] as Function | EventListenerObject; | ||
if (typeof value === 'function') { | ||
(value as Function).call(this.host, event); | ||
} else { | ||
(value as EventListenerObject).handleEvent(event); | ||
} | ||
} | ||
|
||
disconnected() { | ||
const { eventData, element } = this; | ||
for (const key in eventData) { | ||
// event listener | ||
const name = key.slice(1); | ||
const value = eventData[key] as EventListenerWithOptions; | ||
element.removeEventListener(name, this, value); | ||
} | ||
} | ||
|
||
reconnected() { | ||
const { eventData, element } = this; | ||
for (const key in eventData) { | ||
// event listener | ||
const name = key.slice(1); | ||
const value = eventData[key] as EventListenerWithOptions; | ||
element.addEventListener(name, this, value); | ||
} | ||
} | ||
} | ||
|
||
export const spreadEvents = directive(SpreadEventsDirective); | ||
|
||
/** | ||
* Usage: | ||
* import { html, render } from 'lit'; | ||
* import { spread } from '@open-wc/lit-helpers'; | ||
* | ||
* render( | ||
* html` | ||
* <div | ||
* ${spread({ | ||
* 'my-attribute': 'foo', | ||
* '?my-boolean-attribute': true, | ||
* '.myProperty': { foo: 'bar' }, | ||
* '@my-event': () => console.log('my-event fired'), | ||
* })} | ||
* ></div> | ||
* `, | ||
* document.body, | ||
* ); | ||
*/ | ||
export class SpreadDirective extends SpreadEventsDirective { | ||
apply(data: { [key: string]: unknown }) { | ||
if (!data) return; | ||
const { prevData, element } = this; | ||
for (const key in data) { | ||
const value = data[key]; | ||
if (value === prevData[key]) { | ||
continue; | ||
} | ||
const name = key.slice(1); | ||
switch (key[0]) { | ||
case '@': // event listener | ||
this.eventData[name] = value; | ||
this.applyEvent(name, value as EventListenerWithOptions); | ||
break; | ||
case '.': // property | ||
// @ts-ignore | ||
element[name] = value; | ||
break; | ||
case '?': // boolean attribute | ||
if (value) { | ||
element.setAttribute(name, ''); | ||
} else { | ||
element.removeAttribute(name); | ||
} | ||
break; | ||
default: | ||
// standard attribute | ||
if (value != null) { | ||
element.setAttribute(key, String(value)); | ||
} else { | ||
element.removeAttribute(key); | ||
} | ||
break; | ||
} | ||
} | ||
} | ||
|
||
groom(data: { [key: string]: unknown }) { | ||
const { prevData, element } = this; | ||
if (!prevData) return; | ||
for (const key in prevData) { | ||
const name = key.slice(1); | ||
if (!data || (!(key in data) && element[name] === prevData[key])) { | ||
switch (key[0]) { | ||
case '@': // event listener | ||
this.groomEvent(name, prevData[key] as EventListenerWithOptions) | ||
break; | ||
case '.': // property | ||
// @ts-ignore | ||
element[name] = undefined; | ||
break; | ||
case '?': // boolean attribute | ||
element.removeAttribute(name); | ||
break; | ||
default: | ||
// standard attribute | ||
element.removeAttribute(key); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
export const spread = directive(SpreadDirective); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Do we actually need to vend multiple exports/add multiple hard dependencies, or can we just loosen this
peerDependency
to include^2.0.0 || ^3.0.0
so that the consuming project can continue to outline the versions of the dependencies they need rather than forcing both versions that the change does now?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.
sigh I switched laptops and forgot to put the only change that was actually needed... new commit updates the "peerDependencies", and I updated "dependencies" to "devDependencies"