question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

styles typing (auto-completion, validation)

See original GitHub issue

From @caub on March 29, 2018 9:25

I think many of us would like to have syntax highlighting, validation, … for the styles

I’ll talk about VSCode, sorry about other editors, styled-components have https://github.com/styled-components/vscode-styled-components

It should be possible to do it, but more using React definitions: 2018-03-29-112240_1305x489_scrot

And trigger it for a

const styles = {
  foo: {
    alignItems: '...'
  }
};

@visioncan did something interesting: https://github.com/visioncan/vscode-jss-snippets, but I think I’d prefer that it behaves like React inline styles

Copied from original issue: cssinjs/react-jss#218

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:7
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

9reactions
saevarbcommented, May 28, 2019

Hello. I was rather surprised to find out that this hasn’t been implemented considering the claim that there is “first-class support for typescript.”

It seems to me that this has already been solved by Material UI’s createStyles which doesn’t do anything except help the typechecker along. Is there a reason this hasn’t been added to jss?

1reaction
xididricommented, Jan 31, 2021

I ended up re-declaring the function createUseStyles using types from @material-ui/styles/withStyles. This allows to pass the styles object to createUseStyles without declaring a variable first.

// src/types/react-jss.d.ts

import type { StyleRules } from '@material-ui/styles/withStyles';
import type { Classes } from 'jss';

declare module 'react-jss' {

	// redeclare createUseStyles using StyleRules type from material-ui
	// @TODO: use partial inference if possible to allow classes inference (<C>) even when passing <Props>
	function createUseStyles<Props extends object = {}, C extends string = string>(
		styles: StyleRules<Props, C>): (data?: Props) => Classes<C>
}

Use:

// SomeComponent.tsx
import React from 'react';
import { createUseStyles } from 'react-jss';

const useStyles = createUseStyles<{ foo: boolean, bar: number }> = {

	content: {
		height: '100%',
		display: 'flex',
		borderStyle: ({ foo }) => foo ? 'solid': 'none',

		'&:hover': {
			borderWidth: ({ bar }) => bar,
		},
	},
};

const Component = ({ foo }) => {

	const bar = 4;
	const { content } = useStyles({ foo, bar  });

...

One caveat though: classes are not inferred when passing the generic type “argument” (here <{ foo: boolean, bar: number }>), which means if you do const { nonExistingClass } = useStyles({ foo, bar }); tsc will not complain.

If you do not pass the generic type argument (ex if you do not use functional CSS properties in your style object) then classes are correctly inferred and you will get the error.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Types Of Validation with AutoComplete TextBoxes - CodeProject
Today I will show you types of validtaion with different styles which can be done with Textboxes. Here I had also used autocomplete...
Read more >
CSS autofill and validation - javascript - Stack Overflow
When it changes to just "Test", it is invalidated by pattern and causes the CSS to trigger. This means that you can use...
Read more >
Angular Autocomplete Validation - StackBlitz
Angular Material autocomplete option validation.
Read more >
HTML attribute: autocomplete - MDN Web Docs
The HTML autocomplete attribute lets web developers specify what if any permission the user agent has to provide automated assistance in ...
Read more >
Blazor AutoComplete Demos - Validation - Telerik
The Telerik Blazor AutoComplete supports validation and responds to changes in EditContext . It is easily integrated with TelerikForm and Blazor EditForms ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found