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

Format code with prettier #34

Open
siddharthkp opened this issue Sep 21, 2017 · 12 comments
Open

Format code with prettier #34

siddharthkp opened this issue Sep 21, 2017 · 12 comments

Comments

@siddharthkp
Copy link
Contributor

When modifying code on the fly, it would be very useful to have prettier format the code.

I'd be happy to solve my own problem if someone can guide on how to use prettier on the browser.

This is the problem:
ugly

@kitten
Copy link
Contributor

kitten commented Sep 21, 2017

Seems that overflow is giving you trouble?

Try setting “white-space: pre-wrap;” or use “overflow-x: scroll”

@siddharthkp
Copy link
Contributor Author

That would made it wrap at awkward places.

screen shot 2017-09-21 at 4 00 14 pm

prettier does a pretty good job at this.

@siddharthkp
Copy link
Contributor Author

Here's the output with prettier 😍

pretty

@kitten
Copy link
Contributor

kitten commented Sep 21, 2017

@siddharthkp itd be great to have as an opt-in feature 😬 I’m sure not everyone will need it. But I wanted to look into Babel-standalone / custom transpilers anyway, so this would fit right in

@siddharthkp
Copy link
Contributor Author

Good point.

I have no idea what that would look like to be able to help 🙁

If you point me in the right direction, I can give it a shot (after finishing my styleguide)

@tgreen7
Copy link

tgreen7 commented Jan 30, 2018

sorry for the unrelated question, but I was curious how are you injecting the props like that?

@siddharthkp
Copy link
Contributor Author

@tgreen7 here's the bad code if you want to read through: https://github.com/auth0/cosmos/blob/master/src/docs/spec/playground.js

@Yolo390
Copy link

Yolo390 commented Apr 27, 2018

Hey @siddharthkp !
Have you an example of prettier integration with react-live ?
Thanks

@siddharthkp
Copy link
Contributor Author

siddharthkp commented May 2, 2018

@Flo-Slv Here's some hacky code to make it happen:

  1. Including it with good old script tags
  2. Polyfill for the browser
  3. Function call

@Yolo390
Copy link

Yolo390 commented May 4, 2018

@siddharthkp thanks! but doesn't work, i've got this error that i don't understand.
Error: TypeError: parseFunction is not a function

I have see discussions on prettier repos to integrate prettier on browser.

@masterpavan
Copy link

masterpavan commented Aug 19, 2020

If anyone stumbles across this thread, here is how i integrated it with CTRL + L as a hotkey to format:

import prettier from 'prettier/standalone'
import babelParser from 'prettier/parser-babylon'

Then inside your wrapper component:

...

const [code, setCode] = useState(")

const formatOnKey = (e: KeyboardEvent) => {
  if (e.ctrlKey && e.keyCode == 76) {
    setCode(currentCode =>
      prettier.format(currentCode, {
        parser: 'babel',
        plugins: [babelParser]
      }))
  }
}

useEffect(() => {
  document.addEventListener('keyup', formatOnKey, false)
  return () => document.removeEventListener('keyup', formatOnKey, false)
})

...

if you want to do it with a button or something, just use setCode(currentCode => part as your click handler
works on "prettier": "2.0.5"

@rwieruch
Copy link

rwieruch commented Apr 1, 2022

Thanks @masterpavan ! For me the following worked:

import prettier from 'prettier/standalone';
import babelParser from 'prettier/parser-babel';

...

const formattedCode = prettier.format(unformattedCode, {
  parser: 'babel',
  plugins: [babelParser],
});

...

<LiveProvider code={formattedCode}

for applying prettier by default.

Works with "prettier": "2.6.1",

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

No branches or pull requests

6 participants