Skip to content

Commit

Permalink
feat(text-input): Support CSS Variables in InputGroup (#2935)
Browse files Browse the repository at this point in the history
Add support for the new Canvas tokens that utilize CSS Variables in `InputGroup` and export the `maybeWrapCssVars` function from the styling package.

[category:Components]
  • Loading branch information
NicholasBoll authored Sep 24, 2024
1 parent 6f4f525 commit f1fb499
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
7 changes: 4 additions & 3 deletions modules/react/text-input/lib/InputGroup.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';

import {space} from '@workday/canvas-kit-react/tokens';
import {maybeWrapCSSVariables} from '@workday/canvas-kit-styling';
import {system} from '@workday/canvas-tokens-web';
import {
createContainer,
createElemPropsHook,
Expand Down Expand Up @@ -198,11 +199,11 @@ export const InputGroup = createContainer('div')({
// `offsetEnd` arrays
React.Children.forEach(children, child => {
if (React.isValidElement<any>(child) && child.type === InputGroupInnerStart) {
const width = child.props.width || space.xl;
const width = maybeWrapCSSVariables(child.props.width || system.space.x10);
offsetsStart.push(width);
}
if (React.isValidElement<any>(child) && child.type === InputGroupInnerEnd) {
const width = child.props.width || space.xl;
const width = maybeWrapCSSVariables(child.props.width || system.space.x10);
offsetsEnd.push(width);
}
});
Expand Down
12 changes: 10 additions & 2 deletions modules/styling/lib/cs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,17 @@ type DefaultedVarsShape = Record<string, string> | Record<string, Record<string,

/**
* Wrap all unwrapped CSS Variables. For example, `{padding: '--foo'}` will be replaced with
* `{padding: 'var(--foo)'}`. It also works on variables in the middle of the property.
* `{padding: 'var(--foo)'}`. It also works on variables in the middle of the property. Takes any
* string and returns a string with CSS variables wrapped if necesary.
*
* ```ts
* maybeWrapCSSVariables('1rem'); // 1rem
* maybeWrapCSSVariables('--foo'); // var(--foo)
* maybeWrapCSSVariables('var(--foo)'); // var(--foo)
* maybeWrapCSSVariables('calc(--foo)'); // calc(var(--foo))
* ```
*/
function maybeWrapCSSVariables(input: string): string {
export function maybeWrapCSSVariables(input: string): string {
// matches an string starting with `--` that isn't already wrapped in a `var()`. It tries to match
// any character that isn't a valid separator in CSS
return input.replace(
Expand Down

0 comments on commit f1fb499

Please sign in to comment.