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

Typing of custom props in styled #144

Closed
evertheylen opened this issue Nov 17, 2022 · 2 comments
Closed

Typing of custom props in styled #144

evertheylen opened this issue Nov 17, 2022 · 2 comments
Labels
question Further information is requested

Comments

@evertheylen
Copy link

I'd like to inject some custom props into a styled component. The following code actually does what I want at runtime, but it is wrong according to typescript.

const FooComponent = styled('div')<{ foo: string }>(({ props }) => ({
  // this is wrong according to Typescript
  '--foo': props.foo,
}));

// later... (this is also wrong according to Typescript)
<FooComponent foo="bar">

The typescript errors are long and cryptic, and I've spent hours now looking at the underlying types without success. What type parameters do I have to provide for the code below to actually be accepted by typescript?

Stackblitz: https://stackblitz.com/edit/angular-7c4vrj-bzayuw?file=app.tsx

@juanrgm
Copy link
Member

juanrgm commented Nov 17, 2022

This is not possible with mui, you must use the internal ownerstate or define the custom props in a new component.

The only issue is the bad typing with the css variable.

a) Using ownerState.

import styled from '@suid/system/styled';

const FooComponent = styled('div')<{ foo: string }>(({ ownerState }) => ({
  ['--foo' as string]: ownerState.foo,
}));

<FooComponent ownerState={{ foo: 'bar' }} />
}

b) Defining a custom component.

import styled from '@suid/system/styled';
import Box from '@suid/system/Box';
import { JSX } from 'solid-js';

function Foo(props: JSX.IntrinsicElements['div'] & { foo?: string }) {
  return <Box {...props} />;
}

const FooComponent = styled(Foo, {
  skipProps: ['foo'],
})(({ props }) => ({
  ['--foo' as string]: props.foo,
}));

<FooComponent foo="bar" />

@juanrgm juanrgm added the question Further information is requested label Nov 17, 2022
@evertheylen
Copy link
Author

This is not possible with mui

That's too bad, but thanks for the reply!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants