Skip to content

Commit

Permalink
feat: ✨ add css method for dom (antvis#1959)
Browse files Browse the repository at this point in the history
  • Loading branch information
NewByVector authored Apr 1, 2022
1 parent 10ab981 commit e8fbcd8
Show file tree
Hide file tree
Showing 10 changed files with 112 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ dist
*.pem
!mock-cert.pem
tmp
test
6 changes: 6 additions & 0 deletions packages/x6-common/__tests__/dom/attr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@ describe('Dom', () => {
const { id, ...attrs } = vel.attr()
expect(attrs).toEqual({ foo: 'test', bar: '100' })
})

it('should work on html element', () => {
const div = document.createElement('div')
Dom.attr(div, 'data-id', '12')
expect(Dom.attr(div, 'data-id')).toEqual('12')
})
})
})
})
19 changes: 19 additions & 0 deletions packages/x6-common/__tests__/dom/css.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Vector } from '../../src/vector'
import { Dom } from '../../src/dom'

describe('Dom', () => {
describe('css', () => {
it('should set right style property for element', () => {
const vel = Vector.create('rect')
const node = vel.node as Element

Dom.css(node, {
stroke: 'red',
userDrag: 'auto',
})

expect(Dom.css(node, 'stroke')).toEqual('red')
expect(Dom.css(node, 'webkitUserDrag')).toEqual('auto')
})
})
})
2 changes: 1 addition & 1 deletion packages/x6-common/__tests__/dom/prefix.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Dom } from '../../src/dom'
describe('Dom', () => {
describe('#prefix', () => {
it('should return prefixed name with compatibility name', () => {
expect(Dom.getVendorPrefixedName('userDrag')).toBe('WebkitUserDrag')
expect(Dom.getVendorPrefixedName('userDrag')).toBe('webkitUserDrag')
})
})
})
2 changes: 1 addition & 1 deletion packages/x6-common/__tests__/dom/style.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('Dom', () => {
Dom.setPrefixedStyle(style, 'userDrag', 'true')
expect(style).toEqual({
userDrag: 'true',
WebkitUserDrag: 'true',
webkitUserDrag: 'true',
})
})

Expand Down
80 changes: 80 additions & 0 deletions packages/x6-common/src/dom/css.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { getVendorPrefixedName } from './prefix'

const numericProps: { [prop: string]: true | undefined } = {
animationIterationCount: true,
columnCount: true,
flexGrow: true,
flexShrink: true,
fontWeight: true,
gridArea: true,
gridColumn: true,
gridColumnEnd: true,
gridColumnStart: true,
gridRow: true,
gridRowEnd: true,
gridRowStart: true,
lineHeight: true,
opacity: true,
order: true,
orphans: true,
widows: true,
zIndex: true,
}

export function isCSSVariable(prop: string): boolean {
return /^--/.test(prop)
}

export function computeStyle(
elem: Element,
prop: string,
isVariable?: boolean,
) {
const style: any = window.getComputedStyle(elem, null)
return isVariable
? style.getPropertyValue(prop) || undefined
: style[prop] || (elem as any).style[prop]
}

function getSuffixedValue(prop: string, value: number | string) {
return !numericProps[prop] && typeof value === 'number' ? `${value}px` : value
}

export function css(elem: Element, prop: string): string | undefined
export function css(elem: Element, prop: string, value: number | string): void
export function css(elem: Element, prop: Record<string, number | string>): void
export function css(
elem: Element,
prop: string | Record<string, number | string>,
value?: number | string,
) {
if (typeof prop === 'string') {
const isVariable = isCSSVariable(prop)

if (!isVariable) {
prop = getVendorPrefixedName(prop)! // eslint-disable-line
}

if (value === undefined) {
return computeStyle(elem, prop, isVariable)
}

if (!isVariable) {
value = getSuffixedValue(prop, value) // eslint-disable-line
}

const style = (elem as any).style
if (isVariable) {
style.setProperty(prop, value)
} else {
style[prop] = value
}

return
}

// eslint-disable-next-line
for (const key in prop) {
css(elem, key, prop[key])
}
}
1 change: 1 addition & 0 deletions packages/x6-common/src/dom/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export * from './class'
export * from './style'
export * from './prefix'
export * from './selection'
export * from './css'

// svg
// ---
Expand Down
2 changes: 1 addition & 1 deletion packages/x6-common/src/dom/prefix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function camelize(str: string) {
}

const memoized: { [key: string]: string | null } = {}
const prefixes = ['Webkit', 'ms', 'Moz', 'O']
const prefixes = ['webkit', 'ms', 'moz', 'o']
const testStyle = document ? document.createElement('div').style : {}

function getWithPrefix(name: string) {
Expand Down
2 changes: 2 additions & 0 deletions packages/x6-common/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export interface Size {
width: number
height: number
}

export type Nilable<T> = T | null | undefined
export interface KeyValue<T extends any = any> {
[key: string]: T
}
1 change: 0 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"strictNullChecks": true,
"resolveJsonModule": true,
"experimentalDecorators": true,
"importsNotUsedAsValues": "error",
"jsx": "react",
"target": "es5",
"lib": ["DOM", "ES2020"]
Expand Down

0 comments on commit e8fbcd8

Please sign in to comment.