Skip to content

Commit

Permalink
feat: add create-box strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
Amiditin committed Dec 10, 2024
1 parent fac044a commit 99c446f
Showing 1 changed file with 94 additions and 0 deletions.
94 changes: 94 additions & 0 deletions fragments/fragments-generator/src/strategy/create-box.strategy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { Frame } from 'figma-js'
import { createElement } from 'react'

import { ThemeMappingStrategy } from './theme-mapping.strategy.js'

export class CreateBoxStrategy extends ThemeMappingStrategy {
constructor(theme: Record<string, Record<string, string>>) {
super(theme)

Check failure on line 8 in fragments/fragments-generator/src/strategy/create-box.strategy.ts

View workflow job for this annotation

GitHub Actions / Lint

(@typescript-eslint/no-useless-constructor): Useless constructor.

Useless constructor.
Raw output
   5 |
   6 | export class CreateBoxStrategy extends ThemeMappingStrategy {
>  7 |   constructor(theme: Record<string, Record<string, string>>) {
     |   ^
   8 |     super(theme)
   9 |   }
  10 |
}

getPaddingFromTheme(padding: number | undefined) {
if (padding === undefined) {
return undefined
}

return this.getValueKeyFromTheme('spaces', `${padding}px`) || `${padding}px`
}

createPaddingAttributes({
paddingTop,
paddingBottom,
paddingLeft,
paddingRight,
}: Record<string, number | undefined>) {
const top = this.getPaddingFromTheme(paddingTop)
const bottom = this.getPaddingFromTheme(paddingBottom)
const left = this.getPaddingFromTheme(paddingLeft)
const right = this.getPaddingFromTheme(paddingRight)

if ([top, bottom, left, right].every((value) => value && value === top)) {
return { padding: top }
}

const attributes: Record<string, string> = {}

if (top && bottom && top === bottom) {
attributes.paddingY = top
}

if (left && right && left === right) {
attributes.paddingX = left
}

if (top && !attributes.paddingY) {
attributes.paddingTop = top
}

if (bottom && !attributes.paddingY) {
attributes.paddingBottom = bottom
}

if (left && !attributes.paddingX) {
attributes.paddingLeft = left
}

if (right && !attributes.paddingX) {
attributes.paddingRight = right
}

return attributes
}

getImports() {
return [`import { Box } from '@ui/layout'`]
}

createElement(node: Frame) {
const {
layoutMode,
itemSpacing,
counterAxisAlignItems,
primaryAxisAlignItems,
paddingBottom,
paddingLeft,
paddingRight,
paddingTop,
} = node

const figmaAlignItems = {
CENTER: 'center',
SPACE_BETWEEN: 'space-between',
}

return createElement('Box', {
flexDirection: layoutMode === 'VERTICAL' ? 'column' : undefined,
justifyContent: figmaAlignItems[primaryAxisAlignItems!] || undefined,
alignItems: figmaAlignItems[counterAxisAlignItems!] || undefined,
gap: itemSpacing
? this.getValueKeyFromTheme('spaces', `${itemSpacing}px`) || `${itemSpacing}px`
: undefined,
...this.createPaddingAttributes({ paddingBottom, paddingLeft, paddingRight, paddingTop }),
})
}
}

0 comments on commit 99c446f

Please sign in to comment.