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

fix: icon type auto-generation and normalization #696

Merged
merged 11 commits into from
Sep 6, 2024
Merged
12 changes: 11 additions & 1 deletion plugins/ui/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,14 @@ python make_docs.py
```

The files will be built into `docs/build/markdown`.
Note that these built files should not be committed to the repository.
Note that these built files should not be committed to the repository.

## Update Icon Types
Available IconTypes can be generated automatically using icon TypeScript definitions in node_modules.

Writes to `icon_types.py`.

```shell
ethanalvizo marked this conversation as resolved.
Show resolved Hide resolved
cd plugins/ui
ethanalvizo marked this conversation as resolved.
Show resolved Hide resolved
python make_docs.py
```
30 changes: 30 additions & 0 deletions plugins/ui/make_icon_types.py
ethanalvizo marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
relative_path = "./src/js/node_modules/@deephaven/icons/dist/index.d.ts"

icons = []
with open(relative_path, "r") as file:
content = file.read()
words = content.split()
for word in words:
index = words.index(word)
if index < len(words) - 1:
next_word = words[index + 1]
if next_word == "IconDefinition;":
Copy link
Contributor

@bmingles bmingles Aug 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bender we should consider changing something in @deephaven/icons so that we have a separate types file that only includes the icon names. This would help with some .ts scenarios as well so we don't have to exclude the stuff at the end of the file.

...
// end of current icons/dist/index.d.ts
import { IconDefinition, IconLookup, IconName, IconPrefix, IconPack } from '@fortawesome/fontawesome-common-types';
export { IconDefinition, IconLookup, IconName, IconPrefix, IconPack } from '@fortawesome/fontawesome-common-types';
export const prefix: IconPrefix;
export const vs: IconPack;
export const dh: IconPack;

As-is, this is potentially brittle to changes.

icon = word[:-1]
icons.append(icon)

output_file_path = "./src/deephaven/ui/components/types/icon_types.py"

with open(output_file_path, "w") as output_file:
output_file.truncate(0)

output_file.write(
"from __future__ import annotations"
+ "\n"
+ "from typing import Literal"
+ "\n\n"
)
output_file.write("Icons = Literal[" + "\n")
for icon in icons:
output_file.write(' "' + icon + '"\n')

output_file.write("]" + "\n")
3 changes: 2 additions & 1 deletion plugins/ui/src/deephaven/ui/components/icon.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
Position,
IconSize,
IconColor,
IconTypes,
)
from .._internal.utils import create_props


def icon(
name: str,
name: IconTypes,
size: IconSize | None = None,
color: IconColor | None = None,
flex: LayoutFlex | None = None,
Expand Down
1 change: 1 addition & 0 deletions plugins/ui/src/deephaven/ui/components/types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
from .events import *
from .layout import *
from .validate import *
from .icon_types import *
Loading
Loading