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

Writing code to convert foundation interpolation to CSS Variable #1777

Closed
Tracked by #1800
sungik-choi opened this issue Dec 5, 2023 · 0 comments · Fixed by #1781
Closed
Tracked by #1800

Writing code to convert foundation interpolation to CSS Variable #1777

sungik-choi opened this issue Dec 5, 2023 · 0 comments · Fixed by #1781
Assignees
Labels
bezier-codemod Issue or PR related to bezier-codemod enhancement Issues or PR related to making existing features better

Comments

@sungik-choi
Copy link
Contributor

sungik-choi commented Dec 5, 2023

Description

styled backtick 내부의 foundation interpolation을 매칭되는 CSS Variable로 변환하는 codemod 스크립트를 작성합니다.

Reasons for suggestion

styled-components를 제거하면 더 이상 bezier-react에서 Foundation을 ThemeProvider 를 통해 제공하지 않습니다. 이제 CSS Variable(Design token)이 포함된 스타일 시트를 제공하는 형식으로 변경되었으므로, 이를 쉽게 적용할 수 있도록 변환 코드 작성이 필요합니다.

Proposed solution

How to

변환할 토큰의 모든 리스트는 bezier-tokens의 빌드 결과물을 참고해주세요.

export const Box = styled.div`
  /* theme */
  /* AS-IS */
  background-color: ${({ foundation }) => foundation?.theme?.['bg-black-dark']};
  /* TO-BE */
  background-color: var(--bg-black-dark);

  /* subTheme */
  /* AS-IS */
  background-color: ${({ foundation }) => foundation?.subTheme?.['bg-black-dark']};
  /* TO-BE: deprecated! */

  /* spacing */
  /* AS-IS */
  margin: ${({ foundation }) => foundation?.spacing?.s1};
  /* TO-BE: deprecated! */

  /* transition */
  /* AS-IS */
  transition: color 1s ${({ foundation }) => foundation?.transition?.TransitionEasing};
  transition: color ${({ foundation }) => foundation?.transition?.TransitionDuration.S} ease;
  ${({ foundation }) => foundation?.transition?.getTransitionsCSS(['background-color', 'box-shadow'])};
  /* TO-BE */
  transition: color 1s var(--transition-timing-function-default);
  transition: color var(--transition-duration-s) ease;
  transition: background-color var(--transition-s), box-shadow var(--transition-s);

  /* rounding */
  /* AS-IS */
  ${({ foundation }) => foundation?.rounding.round4};
  /* TO-BE */
  overflow: hidden; /* Optional */
  border-radius: var(--radius-4);

  /* elevation */
  /* AS-IS */
  ${({ foundation }) => foundation?.elevation?.ev1()};
  /* TO-BE */
  box-shadow: var(--ev-1);

  /* border */
  /* AS-IS */
  ${({ foundation }) => foundation?.border.getBorder(1, foundation?.theme?.['bdr-black-light'], {
    top: false,
    right: false,
    bottom: true,
    left: true,
  })};
  /* TO-BE */
  border-bottom: 1px solid var(--bdr-black-light); /* Or border-block-end */
`

직접 작업해야하는 부분

  • subTheme : InvertedThemeProvider 로 사용처를 감싸고, subThemetheme 로 변경
  • inverted-*** : InvertedThemeProvider 로 사용처를 감싸고, inverted- prefix를 제거

References

@sungik-choi sungik-choi added the enhancement Issues or PR related to making existing features better label Dec 5, 2023
@github-project-automation github-project-automation bot moved this to 📌 Backlog in Bezier React Dec 5, 2023
@yangwooseong yangwooseong moved this from 📌 Backlog to 🏃‍♀️ In progress in Bezier React Dec 7, 2023
yangwooseong added a commit that referenced this issue Dec 12, 2023
<!--
  How to write a good PR title:
- Follow [the Conventional Commits
specification](https://www.conventionalcommits.org/en/v1.0.0/).
  - Give as much context as necessary and as little as possible
  - Prefix it with [WIP] while it’s a work in progress
-->

## Self Checklist

- [x] I wrote a PR title in **English** and added an appropriate
**label** to the PR.
- [x] I wrote the commit message in **English** and to follow [**the
Conventional Commits
specification**](https://www.conventionalcommits.org/en/v1.0.0/).
- [x] I [added the
**changeset**](https://github.com/changesets/changesets/blob/main/docs/adding-a-changeset.md)
about the changes that needed to be released. (or didn't have to)
- [x] I wrote or updated **documentation** related to the changes. (or
didn't have to)
- [x] I wrote or updated **tests** related to the changes. (or didn't
have to)
- [x] I tested the changes in various browsers. (or didn't have to)
  - Windows: Chrome, Edge, (Optional) Firefox
  - macOS: Chrome, Edge, Safari, (Optional) Firefox

## Related Issue
<!-- Please link to issue if one exists -->

- Fixes #1777

## Summary
<!-- Please brief explanation of the changes made -->

- styled component 의 foundation 객체를 사용한 스타일링을 css variable 로 변환하는
codemod 를 추가합니다.
- 변환 결과의 전 후를 보려면 테스트 코드나 이슈를 참조해주세요. 

## Details
<!-- Please elaborate description of the changes -->

- theme, rounding, border, elevation, transition 에 대하여 개별로 transform 함수를
작성해서, 사용처에서 하나씩 사용하거나 한꺼번에 사용할 수 있게 했습니다. theme 마이그레이션만 해도 몇 백 줄이 변하기
때문에 개별적으로 적용할 수 있게 열어주는 게 좋을 것 같았습니다. spacing 은 데스크 코드에서 사용처가 없기 때문에 손으로
지워도 되리라 생각합니다.

- 어플리케이션에서 테스트 해보려면 @channel.io/bezier-codemod 패키지를 빌드 한 후 아래 명령어로 테스트
가능합니다. 테스트 결과 80\~90% 이상 커버를 해주는 것을 확인했습니다. 동작 속도는 개별 transform 별로
30초~1분 정도 걸립니다.
```
npx bezier-react/packages/bezier-codemod/node_modules/.bin/bezier-codemod
```

- transform 이 커버하지 못하는 코드 조각은 다음과 같습니다. 아래와 같은 코드를 만났을 경우 처리하지 않고 그대로 두는
것이 의도입니다. 이에 대한 테스트 코드 또한 별도로 작성했습니다.

**argument 가 foundation 이외에 여러 개 있을 경우**
```typescript
const div = styled.div`
  ${({ foundation, isRounding }) =>
  isRounding && foundation?.elevation?.ev3()};
`
```

**constant 를 사용하는 경우**
```typescript
  background-color: ${({ foundation }) =>
    foundation?.theme[SOME_CONSTANT]};
```

- transform 이 끝나면 바뀐 파일 갯수를 2배로 보여주는 버그가 있습니다. 아래 코드에서 파일이 두 번씩 들어가서 그런
것 같은데, 아직 정확한 원인은 찾지 못했습니다. 여기서 바로 해결하지 않고 이슈로 남겨두겠습니다.

App.tsx
```typescript
const sourceFiles = project.addSourceFilesAtPaths(filePath)
```

### Breaking change? (Yes/No)
<!-- If Yes, please describe the impact and migration path for users -->

- No

## References
<!-- Please list any other resources or points the reviewer should be
aware of -->

- #1777
- [AST
viewer](https://ts-ast-viewer.com/#code/FAYw9gdgzgLgBAYTAW2QQwgEzgXjrATwBsBTTAOkwEsA3AA2DjgBIBvAClbmqjQCNS2AL4BKXAD5GTblV4CycAGSKpTEFCgNp05YritV2lhy4AzMAFcsaGFUhxREw0bjmrmG3YgB+cjABOGFBUtpDkAOYkMAAqgdAhXlAIAMrJ7ADazi5wAOR8aCAA1uH+llgAtOBEYP45ADRZLjlVNfWNRjlgAA4FIQRt2dIAuiJCWWPadGN0QA)
@github-project-automation github-project-automation bot moved this from 🏃‍♀️ In progress to ✅ Done in Bezier React Dec 12, 2023
@sungik-choi sungik-choi mentioned this issue Dec 15, 2023
@yangwooseong yangwooseong added the bezier-codemod Issue or PR related to bezier-codemod label Dec 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bezier-codemod Issue or PR related to bezier-codemod enhancement Issues or PR related to making existing features better
Projects
No open projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants