Skip to content
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

Add typeof window checks for SSR #277

Open
wants to merge 2 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/UiPopover.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@

<script>
import classlist from './helpers/classlist';
import Drop from 'tether-drop';

let Drop;
if (typeof window !== 'undefined') {
Drop = require('tether-drop');
}

export default {
name: 'ui-popover',
Expand Down
5 changes: 4 additions & 1 deletion src/UiTooltip.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
</template>

<script>
import Tooltip from 'tether-tooltip';
import UUID from './helpers/uuid';
let Tooltip;
if (typeof window !== 'undefined') {
Tooltip = require('tether-tooltip');
}

export default {
name: 'ui-tooltip',
Expand Down
2 changes: 1 addition & 1 deletion src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const config = {

export class KeenUiConfig {
constructor() {
this.data = merge(config, window.KeenUiConfig ? window.KeenUiConfig : {});
this.data = merge(config, (typeof window !== 'undefined' && window.KeenUiConfig) ? window.KeenUiConfig : {});
}

set(config = {}) {
Expand Down
167 changes: 85 additions & 82 deletions src/helpers/modality.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,93 +2,96 @@
* Adapted from https://github.com/alice/modality
* Version: 1.0.2
*/
document.addEventListener('DOMContentLoaded', () => {
let hadKeyboardEvent = false;
const keyboardModalityWhitelist = [
'input:not([type])',
'input[type=text]',
'input[type=number]',
'input[type=date]',
'input[type=time]',
'input[type=datetime]',
'textarea',
'[role=textbox]',
'[supports-modality=keyboard]'
].join(',');

let isHandlingKeyboardThrottle;

const matcher = (() => {
const el = document.body;

if (el.matchesSelector) {
return el.matchesSelector;
}

if (el.webkitMatchesSelector) {
return el.webkitMatchesSelector;
}

if (el.mozMatchesSelector) {
return el.mozMatchesSelector;
}

if (el.msMatchesSelector) {
return el.msMatchesSelector;
}

console.error('Couldn\'t find any matchesSelector method on document.body.');
})();

const disableFocusRingByDefault = function () {
const css = 'body:not([modality=keyboard]) :focus { outline: none; }';
const head = document.head || document.getElementsByTagName('head')[0];
const style = document.createElement('style');

style.type = 'text/css';
style.id = 'disable-focus-ring';

if (style.styleSheet) {
style.styleSheet.cssText = css;
} else {
style.appendChild(document.createTextNode(css));
}

head.insertBefore(style, head.firstChild);
};

const focusTriggersKeyboardModality = function (el) {
let triggers = false;

if (matcher) {
triggers = matcher.call(el, keyboardModalityWhitelist) &&

if (typeof window !== 'undefined') {
document.addEventListener('DOMContentLoaded', () => {
let hadKeyboardEvent = false;
const keyboardModalityWhitelist = [
'input:not([type])',
'input[type=text]',
'input[type=number]',
'input[type=date]',
'input[type=time]',
'input[type=datetime]',
'textarea',
'[role=textbox]',
'[supports-modality=keyboard]'
].join(',');

let isHandlingKeyboardThrottle;

const matcher = (() => {
const el = document.body;

if (el.matchesSelector) {
return el.matchesSelector;
}

if (el.webkitMatchesSelector) {
return el.webkitMatchesSelector;
}

if (el.mozMatchesSelector) {
return el.mozMatchesSelector;
}

if (el.msMatchesSelector) {
return el.msMatchesSelector;
}

console.error('Couldn\'t find any matchesSelector method on document.body.');
})();

const disableFocusRingByDefault = function () {
const css = 'body:not([modality=keyboard]) :focus { outline: none; }';
const head = document.head || document.getElementsByTagName('head')[0];
const style = document.createElement('style');

style.type = 'text/css';
style.id = 'disable-focus-ring';

if (style.styleSheet) {
style.styleSheet.cssText = css;
} else {
style.appendChild(document.createTextNode(css));
}

head.insertBefore(style, head.firstChild);
};

const focusTriggersKeyboardModality = function (el) {
let triggers = false;

if (matcher) {
triggers = matcher.call(el, keyboardModalityWhitelist) &&
matcher.call(el, ':not([readonly])');
}
}

return triggers;
};
return triggers;
};

disableFocusRingByDefault();
disableFocusRingByDefault();

document.body.addEventListener('keydown', () => {
hadKeyboardEvent = true;
document.body.addEventListener('keydown', () => {
hadKeyboardEvent = true;

if (isHandlingKeyboardThrottle) {
clearTimeout(isHandlingKeyboardThrottle);
}
if (isHandlingKeyboardThrottle) {
clearTimeout(isHandlingKeyboardThrottle);
}

isHandlingKeyboardThrottle = setTimeout(() => {
hadKeyboardEvent = false;
}, 100);
}, true);
isHandlingKeyboardThrottle = setTimeout(() => {
hadKeyboardEvent = false;
}, 100);
}, true);

document.body.addEventListener('focus', e => {
if (hadKeyboardEvent || focusTriggersKeyboardModality(e.target)) {
document.body.setAttribute('modality', 'keyboard');
}
}, true);
document.body.addEventListener('focus', e => {
if (hadKeyboardEvent || focusTriggersKeyboardModality(e.target)) {
document.body.setAttribute('modality', 'keyboard');
}
}, true);

document.body.addEventListener('blur', () => {
document.body.removeAttribute('modality');
}, true);
});
document.body.addEventListener('blur', () => {
document.body.removeAttribute('modality');
}, true);
});
}