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

Refactor: Update Behavior of :class to Invert Classes #7

Merged
merged 7 commits into from
Dec 19, 2023

Conversation

jorenrui
Copy link
Contributor

@jorenrui jorenrui commented Dec 17, 2023

Description

Updates the :class attribute to invert classes based on conditional expressions using the ternary operator and/or if statements.

Pre-requisites

This branch uses the new and improved lexer which is more accurate than before and uses the AST traverser to easily traverse and update the code.

Please merge the following IN ORDER:

Setting the Default State

You can now set default states like:

<div class="hidden" :class="shouldShow ? 'visible' : 'hidden'"></div>

The default would be hidden, then when when a shouldShow changes its value it will trigger the :class which will do:

nextClassNames = baseClasses + evaluatedClasses - invertedEvaluatedClasses

Multiple Conditionals

You can also include in multiple conditionals like:

  1. Use multiple ternary operators enclosed in parentheses:
<div
  :class="(selectedTab === 'When' ? 'bg-white shadow-lg' : 'hover:bg-gray-300')
          (whenSelectedTab === 'Dates' ? 'hidden' : '')"
></div>

Lexer Code:

const expr = `(selectedTab === 'When' ? 'bg-white shadow-lg' : 'hover:bg-gray-300')
              (whenSelectedTab === 'Dates' ? ' hidden' : '')`;
const lexer = new Lexer(expr, { isClass: true });
console.log(lexer.output())

Lexer Output:

[ { test: 'selectedTab === \'When\'',
    consequent: '\'bg-white shadow-lg\'',
    alternate: '\'hover:bg-gray-300\'' },
  { test: 'whenSelectedTab === \'Dates\'',
    consequent: '\' hidden\'',
    alternate: '\'\'' } ]
  1. Use if-else statements:
<div
  :class="if (selectedTab === 'When') {
            'bg-white shadow-lg'
          } else {
            'hover:bg-gray-300'
          }

          if (whenSelectedTab === 'Dates') {
            'hidden'
          } else {
            ''
          }"
></div>

Lexer Code:

const expr2 = `if (selectedTab === 'When') {
  const a = "232";
  \`bg-white shadow-lg \${a}\`;
} else {
  'hover:bg-gray-300';
}

if (value == "Test") {
  '2ndif'
} else {
  '2ndelse'
}
`
const lexer2 = new Lexer(expr2, { isClass: true });
console.log(lexer2.output());

Lexer Output:

[ { test: 'selectedTab === \'When\'',
    consequent: '{\n    const a = \'232\';\n    `bg-white shadow-lg ${ a }`;\n}',
    alternate: '{\n    \'hover:bg-gray-300\';\n}' },
  { test: 'value == \'Test\'',
    consequent: '{\n    \'2ndif\';\n}',
    alternate: '{\n    \'2ndelse\';\n}' } ]
📦 Published PR as canary version: 1.0.1-canary.7.0ded757.0

✨ Test out this PR locally via:

npm install [email protected]
# or 
yarn add [email protected]

@jorenrui jorenrui self-assigned this Dec 17, 2023
Base automatically changed from jr.feat-slider to main December 19, 2023 11:44
@jorenrui jorenrui merged commit 0ded757 into main Dec 19, 2023
1 check passed
@jorenrui jorenrui deleted the jr.fix-invert-classes branch December 19, 2023 11:44
Copy link

🚀 PR was released in v1.0.1 🚀

@github-actions github-actions bot added the released This has been released in npm label Feb 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
released This has been released in npm
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant