Skip to content

Commit

Permalink
Merge pull request #124 from tinloof/tin-2703-icon-path-override
Browse files Browse the repository at this point in the history
Icon path override
  • Loading branch information
stilyan-tinloof authored Jan 15, 2025
2 parents df5b64c + 4cf564d commit 624a3ca
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/stupid-rats-scream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tinloof/sanity-studio": patch
---

iconSchema path option
16 changes: 8 additions & 8 deletions packages/sanity-studio/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ npm install @tinloof/sanity-studio
- [Add sections to your Sanity schema](#4-add-sections-to-your-sanity-schema)
- [`documentI18n`](#documenti18n)
- [`localizedItem`](#localizedItem)
- [`defineIcon`](#defineIcon)
- [`iconSchema`](#iconSchema)
- [Disable creation plugin](#disable-creation-plugin)
- [Input with characters count](#input-with-characters-count)

Expand Down Expand Up @@ -454,14 +454,14 @@ The utility will create a nested structure with:
- An "All" option showing all documents of the specified type
- Individual locale options that filter documents by their `locale` field
## `defineIcon`
## `iconSchema`
Builds upon a string field with an options list to show a preview of the icon select as well as other options.
### Basic usage
```tsx
import { defineIcon } from "@tinloof/sanity-studio";
import { iconSchema } from "@tinloof/sanity-studio";
import { defineType } from "sanity";

export default defineType({
Expand All @@ -472,25 +472,25 @@ export default defineType({
type: "string",
name: "title",
},
defineIcon({
name: "icon",
{
...iconSchema,
options: {
list: [
{ title: "Calendar", value: "calendar" },
{ title: "Chat", value: "chat" },
{ title: "Clock", value: "clock" },
],
path: "/icons/select",
},
}),
},
],
});
```
### Parameters
- `options.list`: Uses the default string option list type of `{title: string, value: string}[]`
The ultility searches for icons within the folder `/icons/select/[icon-value].svg`, if you have a Next.js embbedded setup then store them under `/public/icons/select/[icon-value].svg`.
- `options.path`: Path where icons are located
## Disable creation plugin
Expand Down
19 changes: 15 additions & 4 deletions packages/sanity-studio/src/components/IconSelectComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ export function IconSelectComponent(props: IconInputProps): JSX.Element {
const iconOptions = options as IconOptions | undefined;

const iconList = iconOptions?.list ?? [];
let iconsPath = iconOptions?.path ?? "";
// Add / to the end of the path if it's not there
if (!iconsPath.endsWith("/")) {
iconsPath += "/";
}

const [open, setOpen] = useState(false);
const onClose = useCallback(() => setOpen(false), []);
Expand Down Expand Up @@ -125,7 +130,7 @@ export function IconSelectComponent(props: IconInputProps): JSX.Element {
height: "24px",
width: "24px",
}}
src={`/icons/select/${value}.svg`}
src={`${iconsPath}${value}.svg`}
/>
</IconContainer>
<Text>{addSpaceBeforeCapitalLetters(value)}</Text>
Expand Down Expand Up @@ -175,7 +180,7 @@ export function IconSelectComponent(props: IconInputProps): JSX.Element {
onClick={() => onIconChange(icon.value)}
value={icon.value}
>
<BlockVariantCard icon={icon.value} />
<BlockVariantCard icon={icon.value} iconsPath={iconsPath} />
</Flex>
);
})}
Expand All @@ -198,12 +203,18 @@ const IconStyles = {
width: "32px",
};

function BlockVariantCard({ icon }: { icon: string }) {
function BlockVariantCard({
icon,
iconsPath,
}: {
icon: string;
iconsPath: string;
}) {
return (
<BlockVariantCardContainer>
<img
className="select-icon"
src={`/icons/select/${icon}.svg`}
src={`${iconsPath}${icon}.svg`}
style={IconStyles}
/>
<Text style={{ textAlign: "center" }} size={0}>
Expand Down
1 change: 1 addition & 0 deletions packages/sanity-studio/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ export type PathnameInputProps = ObjectFieldProps<SlugValue> & {

export type IconOptions = {
list: { title: string; value: string }[];
path: string;
};

export type IconParams = Omit<
Expand Down

0 comments on commit 624a3ca

Please sign in to comment.