Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(runtime): override attrs set on Host with values from host element (
#4548) This adds a runtime check for attributes set on the host element instance ('from outside') before the attributes set on the `Host` component ('from inside') are set on it. This allows developers to, for instance, override a `role` attribute set on `Host` (see #3052) or to accomplish something similar for any other attribute.
- Loading branch information
1 parent
6484699
commit b088b9e
Showing
9 changed files
with
142 additions
and
8 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
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
15 changes: 15 additions & 0 deletions
15
test/karma/test-app/host-attr-override/host-attr-override.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,15 @@ | ||
import { Component, Host, h } from '@stencil/core'; | ||
|
||
@Component({ | ||
tag: 'host-attr-override', | ||
shadow: true, | ||
}) | ||
export class HostAttrOverride { | ||
render() { | ||
return ( | ||
<Host class="default" role="header"> | ||
<slot></slot> | ||
</Host> | ||
); | ||
} | ||
} |
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,7 @@ | ||
<!DOCTYPE html> | ||
<meta charset="utf8"> | ||
<script src="./build/testapp.esm.js" type="module"></script> | ||
<script src="./build/testapp.js" nomodule></script> | ||
|
||
<host-attr-override class="override"></host-attr-override> | ||
<host-attr-override class="with-role" role="another-role"></host-attr-override> |
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 @@ | ||
import { setupDomTests } from '../util'; | ||
|
||
describe('host attribute overrides', function () { | ||
const { setupDom, tearDownDom } = setupDomTests(document); | ||
let app: HTMLElement; | ||
|
||
beforeEach(async () => { | ||
app = await setupDom('/host-attr-override/index.html'); | ||
}); | ||
afterEach(tearDownDom); | ||
|
||
it('should merge class set in HTML with that on the Host', async () => { | ||
expect(app.querySelector('.default.override')).not.toBeNull(); | ||
}); | ||
|
||
it('should override non-class attributes', () => { | ||
expect(app.querySelector('.with-role').getAttribute('role')).toBe('another-role'); | ||
}); | ||
}); |