Skip to content

Commit d5bebe3

Browse files
dmulatti-compassjosephschmitt
authored andcommitted
Change the way that a CSS rule is added when component is focused (#78)
Change this logic so that instead of appending this rule to whatever the last stylesheet exists in document.styleSheets, create a new stylesheet with this new rule in it. This prevents a bug where if the last loaded stylesheet is from an external resource, it would not allow access to the rules, and throw an error.
1 parent 4055bad commit d5bebe3

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

spec/tests/angularComponent/ngcOmniboxControllerSpec.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,6 @@ describe('ngcOmnibox.angularComponent.ngcOmniboxController', () => {
3232
expect(NgcOmniboxController.$inject.join(',')).toBe('$document,$element,$scope');
3333
});
3434

35-
it('should remove the focus ring when the component is focused', () => {
36-
expect(document.styleSheets[0].insertRule)
37-
.toHaveBeenCalledWith('ngc-omnibox:focus {outline: none}', 0);
38-
});
39-
4035
it('should listen for field element focus events when the field is set', () => {
4136
const fieldEl = Object.assign({}, fakeEl);
4237
spyOn(fieldEl, 'addEventListener');

src/angularComponent/ngcOmniboxController.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,14 @@ export default class NgcOmniboxController {
6464
}, true);
6565

6666
// Remove the focus ring when the overall component is focused
67-
const styleSheets = this.doc.styleSheets;
68-
if (styleSheets && styleSheets.length) {
69-
styleSheets[styleSheets.length - 1].insertRule('ngc-omnibox:focus {outline: none}', 0);
67+
const head = this.doc.head;
68+
const omniboxStyleId = 'ngcOmniboxStyle';
69+
if (head && !this.doc.getElementById(omniboxStyleId)) {
70+
const style = this.doc.createElement('style');
71+
style.id = omniboxStyleId;
72+
style.type = 'text/css';
73+
style.innerText = 'ngc-omnibox:focus {outline: none}';
74+
head.appendChild(style);
7075
}
7176
}
7277

0 commit comments

Comments
 (0)