styleChecker.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import canUseDom from './canUseDom';
  2. export const canUseDocElement = () => canUseDom() && window.document.documentElement;
  3. const isStyleNameSupport = styleName => {
  4. if (canUseDom() && window.document.documentElement) {
  5. const styleNameList = Array.isArray(styleName) ? styleName : [styleName];
  6. const {
  7. documentElement
  8. } = window.document;
  9. return styleNameList.some(name => name in documentElement.style);
  10. }
  11. return false;
  12. };
  13. const isStyleValueSupport = (styleName, value) => {
  14. if (!isStyleNameSupport(styleName)) {
  15. return false;
  16. }
  17. const ele = document.createElement('div');
  18. const origin = ele.style[styleName];
  19. ele.style[styleName] = value;
  20. return ele.style[styleName] !== origin;
  21. };
  22. export function isStyleSupport(styleName, styleValue) {
  23. if (!Array.isArray(styleName) && styleValue !== undefined) {
  24. return isStyleValueSupport(styleName, styleValue);
  25. }
  26. return isStyleNameSupport(styleName);
  27. }
  28. let flexGapSupported;
  29. export const detectFlexGapSupported = () => {
  30. if (!canUseDocElement()) {
  31. return false;
  32. }
  33. if (flexGapSupported !== undefined) {
  34. return flexGapSupported;
  35. }
  36. // create flex container with row-gap set
  37. const flex = document.createElement('div');
  38. flex.style.display = 'flex';
  39. flex.style.flexDirection = 'column';
  40. flex.style.rowGap = '1px';
  41. // create two, elements inside it
  42. flex.appendChild(document.createElement('div'));
  43. flex.appendChild(document.createElement('div'));
  44. // append to the DOM (needed to obtain scrollHeight)
  45. document.body.appendChild(flex);
  46. flexGapSupported = flex.scrollHeight === 1; // flex container should be 1px high from the row-gap
  47. document.body.removeChild(flex);
  48. return flexGapSupported;
  49. };
  50. export default isStyleSupport;