-
Notifications
You must be signed in to change notification settings - Fork 1k
refactor(web_core): centralize layout mapping logic #1030
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| /* | ||
| * Copyright 2025 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| /** | ||
| * Centralized layout mapping utilities for Web/CSS-based renderers. | ||
| * | ||
| * Maps A2UI layout enum values (e.g., `spaceBetween`) to their corresponding | ||
| * CSS values (e.g., `space-between`). These functions are shared across all | ||
| * web renderers (React, Lit, Angular) to ensure consistent behavior. | ||
| */ | ||
|
|
||
| /** | ||
| * Maps an A2UI justify enum value to its CSS `justify-content` equivalent. | ||
| * | ||
| * @param value - An A2UI justify value such as `'start'`, `'center'`, | ||
| * `'spaceBetween'`, etc. | ||
| * @returns The corresponding CSS `justify-content` value. Defaults to | ||
| * `'flex-start'` for unrecognized or undefined input. | ||
| */ | ||
| export function mapJustify(value?: string): string { | ||
| switch (value) { | ||
| case 'center': | ||
| return 'center'; | ||
| case 'end': | ||
| return 'flex-end'; | ||
| case 'spaceAround': | ||
| return 'space-around'; | ||
| case 'spaceBetween': | ||
| return 'space-between'; | ||
| case 'spaceEvenly': | ||
| return 'space-evenly'; | ||
| case 'start': | ||
| return 'flex-start'; | ||
| case 'stretch': | ||
| return 'stretch'; | ||
| default: | ||
| return 'flex-start'; | ||
| } | ||
| } | ||
|
Comment on lines
+43
to
+45
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For improved conciseness and maintainability, you could consider using an object map for this mapping instead of a For example, you could define the maps at the module level and then reference them in the functions: const justifyMap: Record<string, string> = {
center: 'center',
end: 'flex-end',
spaceAround: 'space-around',
spaceBetween: 'space-between',
spaceEvenly: 'space-evenly',
start: 'flex-start',
stretch: 'stretch',
};
export function mapJustify(value?: string): string {
return (value && justifyMap[value]) || 'flex-start';
}
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done, refactored both functions to use Record<string, string> object maps (470fe86). |
||
|
|
||
| /** | ||
| * Maps an A2UI align enum value to its CSS `align-items` equivalent. | ||
| * | ||
| * @param value - An A2UI align value such as `'start'`, `'center'`, `'end'`, | ||
| * or `'stretch'`. | ||
| * @returns The corresponding CSS `align-items` value. Defaults to `'stretch'` | ||
| * for unrecognized or undefined input. | ||
| */ | ||
| export function mapAlign(value?: string): string { | ||
| switch (value) { | ||
| case 'start': | ||
| return 'flex-start'; | ||
| case 'center': | ||
| return 'center'; | ||
| case 'end': | ||
| return 'flex-end'; | ||
| case 'stretch': | ||
| return 'stretch'; | ||
| default: | ||
| return 'stretch'; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to the repository style guide (line 17), new code changes should be accompanied by tests. These new utility functions are a core part of the layout logic and should have unit tests to verify their mapping behavior, including handling of default and undefined values.
References
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, added 15 unit tests for both functions in layout.test.ts (470fe86).