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: custom chart colors support within existing colors prop #843

Merged
merged 20 commits into from
Dec 10, 2023
Merged

feat: custom chart colors support within existing colors prop #843

merged 20 commits into from
Dec 10, 2023

Conversation

adamhenson
Copy link
Contributor

@adamhenson adamhenson commented Dec 9, 2023

Description

This change is refactor of #836 to provide support for custom colors, but instead of the additive customChartColors prop - this ports functionality to the existing colors prop.

With this change, the colors prop can accept custom colors or tremor base colors ((Color | string)[]). customChartColors is removed in this change.

Example Usage

<LineChart
  {...props}
  colors={[
    // now supports any mix of tremor base colors...
    'emerald',
    'indigo',

    // ... and custom colors
    '#32a852',
    'custom-color-100',
  ]}
/>

Example safelist needed in Tailwind config to support the above implementation:

safelist: [
  // ..
  // ..
  // custom colors
  ...["[#32a852]", "custom-color-100"].flatMap((customColor) => [
    `bg-${customColor}`,
    `border-${customColor}`,
    `hover:bg-${customColor}`,
    `hover:border-${customColor}`,
    `hover:text-${customColor}`,
    `fill-${customColor}`,
    `ring-${customColor}`,
    `stroke-${customColor}`,
    `text-${customColor}`,
    `ui-selected:bg-${customColor}`,
    `ui-selected:border-${customColor}`,
    `ui-selected:text-${customColor}`,
  ]),
],

Updated Components

  • AreaChart
  • BarChart
  • DonutChart
  • LineChart
  • ScatterChart
  • SparkAreaChart
  • SparkBarChart
  • SparkLineChart

Related issues and PRs

What kind of change does this PR introduce?

Although this change updates the existing colors prop, the functionality is additive and doesn't break existing behavior.

  • Bug fix (non-breaking change which fixes an issue)
  • New Feature (non-breaking change which adds functionality)
  • Refactor
  • Build-related changes
  • Other, please describe:

Does this PR introduce a breaking change? (check one)

  • Yes
  • No

How has This been tested?

Storybook stories have been added for all components demonstrating new support of custom colors in the colors prop.

The PR fulfills these requirements:

  • It's submitted to the main branch
  • When resolving a specific issue, it's referenced in the related issue section above
  • My change requires a change to the documentation. (Managed by Tremor Team)
  • I have added tests to cover my changes
  • Check the "Allow edits from maintainers" option while creating your PR.
  • Add refs #XXX or fixes #XXX to the related issue section if your PR refers to or fixes an issue.
  • By contributing to Tremor, you confirm that you have read and agreed to Tremor's CONTRIBUTING.md guideline. You also agree that your contributions will be licensed under the Apache License 2.0 license.

Copy link

vercel bot commented Dec 9, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
tremor-storybook ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 9, 2023 8:18pm

@severinlandolt
Copy link
Member

This PR is what open-source maintainers dream of🫶 Will go to bed now and review it tomorrow!

@adamhenson
Copy link
Contributor Author

Hah! Awesome, thanks @severinlandolt - appreciated 🙏

@severinlandolt severinlandolt changed the base branch from beta to beta-customcolors December 10, 2023 11:49
@severinlandolt severinlandolt added the PR: In Review This PR is in the process of being reviewed by the team label Dec 10, 2023
@severinlandolt severinlandolt merged commit 5752d25 into tremorlabs:beta-customcolors Dec 10, 2023
20 checks passed
Copy link

🎉 This PR is included in version 3.12.0-beta-customcolors.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

@severinlandolt
Copy link
Member

severinlandolt commented Dec 10, 2023

Hi @adamhenson, I just released a new beta (beta-customcolors) and testing it on our website. Two small questions:

  1. Legend
    I think we do not need the customColor prop in the svg anymore, or am I missing something?
    CleanShot 2023-12-10 at 13 49 07@2x

  2. Interestingly, I use Tremor together with MDXRemote, and there is a TS error popping up. I am not 100% sure if this is a problem of Tremor or MDXRemote. I will do some tests.

Update: Adding export const colorValues: readonly string[] = [... and remove as const; assertion to the colorValues array does not work.
Error:
CleanShot 2023-12-10 at 13 54 49@2x

CleanShot 2023-12-10 at 13 52 12@2x

@adamhenson
Copy link
Contributor Author

Thanks @severinlandolt! And thanks for testing. Yes, correct, we don't need customColor... I must have missed that one. Regarding the second issue - I wonder if there is a way I can reproduce 🤔

In the meantime, I'll open a branch against beta-customcolors to fix that first issue.

@adamhenson
Copy link
Contributor Author

Would you be able to provide the full error @severinlandolt?

@severinlandolt
Copy link
Member

severinlandolt commented Dec 10, 2023

Sure @adamhenson,

Type '{ Accordion: ForwardRefExoticComponent<tremor.AccordionProps & RefAttributes<HTMLDivElement>>; AccordionBody: ForwardRefExoticComponent<...>; ... 180 more ...; blockquote: (props: any) => JSX.Element; }' is not assignable to type 'MDXComponents | MergeComponents | null | undefined'.
  Type '{ Accordion: ForwardRefExoticComponent<tremor.AccordionProps & RefAttributes<HTMLDivElement>>; AccordionBody: ForwardRefExoticComponent<...>; ... 180 more ...; blockquote: (props: any) => JSX.Element; }' is not assignable to type 'MDXComponents'.
    Type '{ Accordion: ForwardRefExoticComponent<tremor.AccordionProps & RefAttributes<HTMLDivElement>>; AccordionBody: ForwardRefExoticComponent<...>; ... 180 more ...; blockquote: (props: any) => JSX.Element; }' is not assignable to type 'NestedMDXComponents'.
      Property 'colorValues' is incompatible with index signature.
        Type 'readonly ["slate", "gray", "zinc", "neutral", "stone", "red", "orange", "amber", "yellow", "lime", "green", "emerald", "teal", "cyan", "sky", "blue", "indigo", "violet", "purple", "fuchsia", "pink", "rose"]' is not assignable to type 'NestedMDXComponents | Component<any>'.
          Type 'readonly ["slate", "gray", "zinc", "neutral", "stone", "red", "orange", "amber", "yellow", "lime", "green", "emerald", "teal", "cyan", "sky", "blue", "indigo", "violet", "purple", "fuchsia", "pink", "rose"]' is not assignable to type 'NestedMDXComponents'.
            Index signature for type 'string' is missing in type 'readonly ["slate", "gray", "zinc", "neutral", "stone", "red", "orange", "amber", "yellow", "lime", "green", "emerald", "teal", "cyan", "sky", "blue", "indigo", "violet", "purple", "fuchsia", "pink", "rose"]'.ts(2322)
index.d.ts(30, 5): The expected type comes from property 'components' which is declared here on type 'IntrinsicAttributes & MDXRemoteSerializeResult<Record<string, unknown>, Record<string, unknown>> & { components?: MDXComponents | MergeComponents | null | undefined; lazy?: boolean | undefined; }'

Using npm i @tremor/react@3.12.0-beta-customcolors.4

I try to create a small repo to replicate the issue in the meantime

@adamhenson
Copy link
Contributor Author

adamhenson commented Dec 10, 2023

Also, with that said this could be a breaking change in types if Color type is being imported and expected to be colors: Color[]. I could see indexing being a problem. I am surprised the colorValues: readonly string[] change didn't work. But still digging in.

I'm wondering if we should do something like this and replace all occurrences of (Color | string)[].

- export type Color = (typeof colorValues)[number];
+ export type Color = (typeof colorValues)[number] | string;

EDIT: You can disregard the above - was just thinking. And I still don't think this should be a breaking change. The error is a little confusing because it says Property 'colorValues' is incompatible with index signature. while we don't use colorValues in other files (we can remove export). I'll look forward to the repro - if possible, otherwise continuing to brainstorm.

@severinlandolt
Copy link
Member

severinlandolt commented Dec 10, 2023

Hey @adamhenson, I added you as an outside collaborator to this repo so feel free to directly push to https://github.com/tremorlabs/tremor/tree/beta-customcolors

Working on the reproduction now (ps: other repos of mine work fine)

@adamhenson
Copy link
Contributor Author

Great, thanks @severinlandolt!

@adamhenson
Copy link
Contributor Author

adamhenson commented Dec 10, 2023

Another thought I had is maybe the build tool from the repro website doesn't like my type casting in this one.

export const getIsBaseColor = (color: Color | string) => colorValues.includes(color as Color);

But will wait to look at the repro.

EDIT: that doesn't really make sense either if it's built already. I'll just hold my horses 🏇

@severinlandolt
Copy link
Member

severinlandolt commented Dec 10, 2023

Alright, sent you another invitation 📨 dev works, build not

Culprit: components/common/decoration/Markdown/Markdown.tsx

@adamhenson
Copy link
Contributor Author

Perfect, thanks @severinlandolt 👀

@severinlandolt
Copy link
Member

@adamhenson Feel free to make as many beta releases of https://github.com/tremorlabs/tremor/tree/beta-customcolors as you wish. Just prefix your commit with fix: and a new beta should be released

@adamhenson
Copy link
Contributor Author

Oh awesome, thanks @severinlandolt! I might be able to get away with local testing via npm link but if not, that will be super helpful.

@adamhenson
Copy link
Contributor Author

adamhenson commented Dec 10, 2023

I have a theory that it's because I added export to export colorValues and it looks like it's being exported from the main package and the import is expected to be a component in this project. About to test this now.

From the built index.d.ts from the @tremor/react package.

Screenshot 2023-12-10 at 12 36 25 PM

I can remove that export statement anyways, since I'm no longer using it in other files. My theory is that this will fix it. More coming soon... just need to test.

This imports everything...

import * as tremor from "@tremor/react";

and then...

<MDXRemote
  {...source}
  components={{
    ...getRenderersByContext(context),
    ...tremor,
  }}
/>

@severinlandolt
Copy link
Member

True, in the previous version the type in question was not exported!

@adamhenson
Copy link
Contributor Author

@severinlandolt, yep - 3.12.0-beta-customcolors.5 fixes! I just removed the export - it was no longer needed anyways. I deleted my previous comment because that error might have been a bi-product of npm link. I ran the build with the new release and build completes successfully. I pushed to the bug repo too with the updated version.

@adamhenson
Copy link
Contributor Author

adamhenson commented Dec 10, 2023

Phew, thanks for all the help here @severinlandolt. And let me know if I can do anything else.

@severinlandolt
Copy link
Member

severinlandolt commented Dec 10, 2023

No, I have to thank you! I will test the new beta in my repos asap🚀 Crazy what one single export can cause in side effects

@adamhenson
Copy link
Contributor Author

adamhenson commented Dec 10, 2023

Hah! Yep, the main entrypoint of the package exports everything from ./lib/inputTypes.

export * from "./lib/inputTypes";

And the repro project expects all those imports to be components 🤷 (or something like that... maybe all non-type exports)

// wildcards are a little dangerous for this reason
import * as tremor from "@tremor/react";

// ...

<MDXRemote
  {...source}
  components={{
    ...getRenderersByContext(context),

    // this is where i think the assumption is that all imports are components
    ...tremor,
  }}
/>

I get why this is like this... I mean you wouldn't want to have to update the imports explicitly anytime a component is added.

severinlandolt added a commit that referenced this pull request Dec 10, 2023
* feat: custom chart colors support within existing `colors` `prop` (#843)

---------

Co-authored-by: Adam <adamhenson1979@gmail.com>
severinlandolt added a commit that referenced this pull request Dec 14, 2023
* feat: Add tooltip to switch component (#819)

* chore: tracker facelift (#839)

* feat: Custom Chart Color (#845)

* feat: custom chart colors support within existing `colors` `prop` (#843)

* Update tailwind.config.js

* Update pull_request_template.md

---------

Co-authored-by: motinados <69296148+motinados@users.noreply.github.com>
Co-authored-by: Maxime BAUCHET <maxime.bauchet@insystem.fr>
Co-authored-by: Adam <adamhenson1979@gmail.com>
Co-authored-by: Rajdip Bhattacharya <agentR47@gmail.com>
Co-authored-by: christopherkindl <53372002+christopherkindl@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR: In Review This PR is in the process of being reviewed by the team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants