import {kebabCase} from 'lodash-es' import {StringIndexedObject} from '../ts-helper' import {hasOwnProperty} from '../help' /** * 强制重绘 * this may be not worked in safari, try `forceRedrawAlt()` instead * reference: https://stackoverflow.com/a/3485654/3040605 * @param elem */ export function forceRedraw(elem: HTMLElement) { const originalStyle = elem.style.display elem.style.display = 'none' elem.offsetHeight // no need to store this anywhere, the reference is enough elem.style.display = originalStyle } /** * 强制重绘 alt * reference: https://stackoverflow.com/a/3485654/3040605 * @param elem */ export function forceRedrawAlt(elem: HTMLElement) { const originalStyle = elem.style.transform elem.style.transform = 'translateZ(0)' setTimeout(() => { elem.style.transform = originalStyle }) } /** * 对 element 设置一个/一组 inline style * @param elem * @param propNameOrSet * @param value */ export function setStyle( elem: HTMLElement, propNameOrSet: string | StringIndexedObject, value?: string | number ) { if (typeof propNameOrSet === 'object') { const propSet = propNameOrSet for (const propName in propSet) { if (hasOwnProperty(propSet, propName)) { setStyle(elem, propName, propSet[propName] as string | number) } } return } const name = kebabCase(propNameOrSet) if (typeof value !== 'undefined') { value = typeof value === 'number' ? `${value}px` : value // https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleDeclaration/setProperty elem.style.setProperty(name, value) // 现代浏览器对 transition/transform/animation 等已无需 vendor 前缀,故不再附带带前缀的属性; // 下方 getVendorPrefix / getVendorPropName 等仅作为预留 API 保留(当前无使用方)。 } } export function getStyle(elem: HTMLElement, propName: string) { return elem.style.getPropertyValue(kebabCase(propName)) } // brick 细条滚动条(style 的 `scrollbar()` mixin,scrollbar-width: thin)的 ::-webkit-scrollbar // 兜底宽,对齐 scss `$scrollbar-thin-width: 6px`。供 `thin: true` 量「细条」占位宽时用。 const BRICK_SCROLLBAR_THIN_WEBKIT_WIDTH = 6 export function getBrowserScrollBarWidth(options?: {thin?: boolean; thinWebkitWidth?: number}) { // 可能在 ssr 环境中被调用 // istanbul ignore next if (typeof document === 'undefined') { return 0 } const scrollDiv = document.createElement('div') // 量「细条」占位宽时,让探针与目标容器一致地变细:现代浏览器用 scrollbar-width:thin, // Safari 17 / 老 WebKit 不支持该属性,改注入一条 ::-webkit-scrollbar 宽度规则兜底。 // overlay 滚动条(如 macOS 默认)两种浏览器都量得 0,天然无需补偿。缺省=旧的默认滚动条量法。 // `thin: true` 是给用了 `scrollbar()` mixin 的组件(如 Table)量细条占位宽的语义入口, // 等价于 `thinWebkitWidth: 6`;`thinWebkitWidth` 保留以兼容显式指定兜底宽的调用(如 picker)。 const thinWebkitWidth = typeof options?.thinWebkitWidth === 'number' ? options.thinWebkitWidth : (options?.thin ? BRICK_SCROLLBAR_THIN_WEBKIT_WIDTH : undefined) let probeStyle: HTMLStyleElement | null = null if (typeof thinWebkitWidth === 'number') { scrollDiv.style.scrollbarWidth = 'thin' scrollDiv.className = 'brick-scrollbar-probe' probeStyle = document.createElement('style') probeStyle.textContent = `.brick-scrollbar-probe::-webkit-scrollbar{width:${thinWebkitWidth}px}` document.head.appendChild(probeStyle) } const $body = document.body Object.assign(scrollDiv.style, { position: 'absolute', top: '-9999px', width: '50px', height: '50px', overflow: 'scroll', }) $body.appendChild(scrollDiv) const scrollBarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth $body.removeChild(scrollDiv) if (probeStyle) { document.head.removeChild(probeStyle) } return scrollBarWidth } // 预留方案:vendor 前缀相关 helper。现代浏览器已无需前缀,仓库内暂无使用方, // 仅作为 @befe/brick-utils 对外 API 保留。 const vendorPrefixMap: StringIndexedObject = { Webkit: '-webkit-', Moz: '-moz-', ms: '-ms-', } let vendorPrefix: string export function getVendorPrefix() { if (typeof vendorPrefix !== 'undefined') { return vendorPrefix } vendorPrefix = '' const style = document.createElement('div').style for (const key in vendorPrefixMap) { if (`${key}Transform` in style) { vendorPrefix = vendorPrefixMap[key] } } return vendorPrefix } export const useVendorStyleProps: string[] = [ 'transition', 'transition-delay', 'transition-duration', 'transition-property', 'transition-timing-function', 'transform', 'transform-origin', 'animation', 'animation-delay', 'animation-direction', 'animation-duration', 'animation-fill-mode', 'animation-iteration-count', 'animation-name', 'animation-play-state', 'animation-timing-function', ] export function getVendorPropName(name: string) { if (useVendorStyleProps.includes(name)) { return `${getVendorPrefix()}${name}` } return name }