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

feat(text-input): Support CSS Variables in InputGroup #2935

Merged
merged 3 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading