resolve-variants.mjs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. function getValueState(visualElement) {
  2. const state = [{}, {}];
  3. visualElement?.values.forEach((value, key) => {
  4. state[0][key] = value.get();
  5. state[1][key] = value.getVelocity();
  6. });
  7. return state;
  8. }
  9. function resolveVariantFromProps(props, definition, custom, visualElement) {
  10. /**
  11. * If the variant definition is a function, resolve.
  12. */
  13. if (typeof definition === "function") {
  14. const [current, velocity] = getValueState(visualElement);
  15. definition = definition(custom !== undefined ? custom : props.custom, current, velocity);
  16. }
  17. /**
  18. * If the variant definition is a variant label, or
  19. * the function returned a variant label, resolve.
  20. */
  21. if (typeof definition === "string") {
  22. definition = props.variants && props.variants[definition];
  23. }
  24. /**
  25. * At this point we've resolved both functions and variant labels,
  26. * but the resolved variant label might itself have been a function.
  27. * If so, resolve. This can only have returned a valid target object.
  28. */
  29. if (typeof definition === "function") {
  30. const [current, velocity] = getValueState(visualElement);
  31. definition = definition(custom !== undefined ? custom : props.custom, current, velocity);
  32. }
  33. return definition;
  34. }
  35. export { resolveVariantFromProps };