-
Notifications
You must be signed in to change notification settings - Fork 795
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(compiler): add deletegatesFocus to custom elements targets (#3117)
this commit allows `delegatesFocus` to be properly applied to components generated using the following output targets: - dist-custom-elements - dist-custom-elements-bundle the generation of the `attachShadow` call is moved from a standalone function to being attached to the prototype of the custom element when we proxy it. the reason for this is that we need the component metadata to to determine whether or not each individual component should have delegateFocus enabled or not. this led to the removal of the original standalone attachShadow function. I do not consider this to be a breaking change, as we don't publicly state our runtime APIs are available for general consumption. this change also led to the transition from using ts.create*() calls to ts.factory.create*() calls for nativeAttachShadowStatement, which is the general direction I'd like to take such calls, since the former is now deprecated STENCIL-90: "dist-custom-elements-bundle" does not set delegatesFocus when attaching shadow
- Loading branch information
1 parent
63dbb47
commit 2ffb503
Showing
14 changed files
with
243 additions
and
16 deletions.
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
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,70 @@ | ||
import { mockCompilerCtx } from '@stencil/core/testing'; | ||
import * as d from '@stencil/core/declarations'; | ||
import { transpileModule } from './transpile'; | ||
import { nativeComponentTransform } from '../component-native/tranform-to-native-component'; | ||
|
||
describe('nativeComponentTransform', () => { | ||
let compilerCtx: d.CompilerCtx; | ||
let transformOpts: d.TransformOptions; | ||
|
||
beforeEach(() => { | ||
compilerCtx = mockCompilerCtx(); | ||
transformOpts = { | ||
coreImportPath: '@stencil/core', | ||
componentExport: 'customelement', | ||
componentMetadata: null, | ||
currentDirectory: '/', | ||
proxy: null, | ||
style: 'static', | ||
styleImportData: undefined, | ||
}; | ||
}); | ||
|
||
describe('updateNativeComponentClass', () => { | ||
it("adds __attachShadow() calls when a component doesn't have a constructor", () => { | ||
const code = ` | ||
@Component({ | ||
tag: 'cmp-a', | ||
shadow: true, | ||
}) | ||
export class CmpA { | ||
@Prop() foo: number; | ||
} | ||
`; | ||
|
||
const transformer = nativeComponentTransform(compilerCtx, transformOpts); | ||
|
||
const transpiledModule = transpileModule(code, null, compilerCtx, null, [], [transformer]); | ||
|
||
expect(transpiledModule.outputText).toContain( | ||
`import { attachShadow as __stencil_attachShadow, defineCustomElement as __stencil_defineCustomElement } from "@stencil/core";` | ||
); | ||
expect(transpiledModule.outputText).toContain(`this.__attachShadow()`); | ||
}); | ||
|
||
it('adds __attachShadow() calls when a component has a constructor', () => { | ||
const code = ` | ||
@Component({ | ||
tag: 'cmp-a', | ||
shadow: true, | ||
}) | ||
export class CmpA { | ||
@Prop() foo: number; | ||
constructor() { | ||
super(); | ||
} | ||
} | ||
`; | ||
|
||
const transformer = nativeComponentTransform(compilerCtx, transformOpts); | ||
|
||
const transpiledModule = transpileModule(code, null, compilerCtx, null, [], [transformer]); | ||
|
||
expect(transpiledModule.outputText).toContain( | ||
`import { attachShadow as __stencil_attachShadow, defineCustomElement as __stencil_defineCustomElement } from "@stencil/core";` | ||
); | ||
expect(transpiledModule.outputText).toContain(`this.__attachShadow()`); | ||
}); | ||
}); | ||
}); |
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
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
18 changes: 18 additions & 0 deletions
18
test/karma/test-app/custom-elements-delegates-focus/custom-elements-delegates-focus.tsx
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,18 @@ | ||
import { Component, Host, h } from '@stencil/core'; | ||
|
||
@Component({ | ||
tag: 'custom-elements-delegates-focus', | ||
styleUrl: 'shared-delegates-focus.css', | ||
shadow: { | ||
delegatesFocus: true, | ||
}, | ||
}) | ||
export class CustomElementsDelegatesFocus { | ||
render() { | ||
return ( | ||
<Host> | ||
<input /> | ||
</Host> | ||
); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
test/karma/test-app/custom-elements-delegates-focus/custom-elements-no-delegates-focus.tsx
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,16 @@ | ||
import { Component, Host, h } from '@stencil/core'; | ||
|
||
@Component({ | ||
tag: 'custom-elements-no-delegates-focus', | ||
styleUrl: 'shared-delegates-focus.css', | ||
shadow: true, | ||
}) | ||
export class CustomElementsNoDelegatesFocus { | ||
render() { | ||
return ( | ||
<Host> | ||
<input /> | ||
</Host> | ||
); | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
test/karma/test-app/custom-elements-delegates-focus/index.esm.js
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,3 @@ | ||
import { defineCustomElement } from '../../test-components/custom-elements-delegates-focus'; | ||
|
||
defineCustomElement(); |
6 changes: 6 additions & 0 deletions
6
test/karma/test-app/custom-elements-delegates-focus/index.html
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,6 @@ | ||
<!DOCTYPE html> | ||
<meta charset="utf8"> | ||
<script src="/custom-elements-delegates-focus/main.js"></script> | ||
|
||
<custom-elements-delegates-focus></custom-elements-delegates-focus> | ||
<custom-elements-no-delegates-focus></custom-elements-no-delegates-focus> |
31 changes: 31 additions & 0 deletions
31
test/karma/test-app/custom-elements-delegates-focus/karma.spec.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { setupDomTests } from '../util'; | ||
|
||
describe('custom-elements-delegates-focus', () => { | ||
const { setupDom, tearDownDom } = setupDomTests(document); | ||
let app: HTMLElement; | ||
|
||
beforeEach(async () => { | ||
app = await setupDom('/custom-elements-delegates-focus/index.html'); | ||
}); | ||
afterEach(tearDownDom); | ||
|
||
it('sets delegatesFocus correctly', async () => { | ||
expect(customElements.get('custom-elements-delegates-focus')).toBeDefined(); | ||
|
||
const elm: Element = app.querySelector('custom-elements-delegates-focus'); | ||
|
||
expect(elm.shadowRoot).toBeDefined(); | ||
// as of TypeScript 4.3, `delegatesFocus` does not exist on the `shadowRoot` object | ||
expect((elm.shadowRoot as any).delegatesFocus).toBe(true); | ||
}); | ||
|
||
it('does not set delegatesFocus when shadow is set to "true"', async () => { | ||
expect(customElements.get('custom-elements-no-delegates-focus')).toBeDefined(); | ||
|
||
const elm: Element = app.querySelector('custom-elements-no-delegates-focus'); | ||
|
||
expect(elm.shadowRoot).toBeDefined(); | ||
// as of TypeScript 4.3, `delegatesFocus` does not exist on the `shadowRoot` object | ||
expect((elm.shadowRoot as any).delegatesFocus).toBe(false); | ||
}); | ||
}); |
15 changes: 15 additions & 0 deletions
15
test/karma/test-app/custom-elements-delegates-focus/shared-delegates-focus.css
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,15 @@ | ||
:host { | ||
display: block; | ||
border: 5px solid red; | ||
padding: 10px; | ||
margin: 10px; | ||
} | ||
|
||
:host(:focus) { | ||
border: 5px solid green; | ||
} | ||
|
||
input { | ||
display: block; | ||
width: 100%; | ||
} |
19 changes: 19 additions & 0 deletions
19
test/karma/test-app/custom-elements-delegates-focus/webpack.config.js
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,19 @@ | ||
const path = require('path'); | ||
|
||
module.exports = { | ||
entry: path.resolve(__dirname, 'index.esm.js'), | ||
output: { | ||
path: path.resolve(__dirname, '..', '..', 'www', 'custom-elements-delegates-focus'), | ||
publicPath: '/custom-elements-delegates-focus/', | ||
}, | ||
mode: 'production', | ||
optimization: { | ||
minimize: false, | ||
}, | ||
resolve: { | ||
alias: { | ||
'@stencil/core/internal/client': '../../../internal/client', | ||
'@stencil/core/internal/app-data': '../app-data', | ||
}, | ||
}, | ||
}; |