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

[Docs] Update all code blocks to toggle between TS and JS syntax #4112

Open
markerikson opened this issue Jun 22, 2021 · 8 comments
Open

[Docs] Update all code blocks to toggle between TS and JS syntax #4112

markerikson opened this issue Jun 22, 2021 · 8 comments
Labels

Comments

@markerikson
Copy link
Contributor

The RTK docs currently use Lenz Weber's https://github.com/phryneas/remark-typescript-tools plugin to let us write TS syntax in code blocks, compile away the TS syntax to plain JS, and show both versions as a tab, as seen at https://redux-toolkit.js.org/api/createSlice:

image

We ought to start doing the same thing for the RTK docs.

The relevant RTK docs build config is at https://github.com/reduxjs/redux-toolkit/blob/818efb9c9e46df23c891feaab869d72f2d9ecc35/website/docusaurus.config.js#L12-L53 .

We might also want to look at https://shikijs.github.io/twoslash/ , which is built by one of the TS devs. Lenz has said he'd like to investigate switching the RTK docs over to that at some point, so worth considering now.

@timdorr
Copy link
Member

timdorr commented Jun 22, 2021

Does the JS come out looking weird at all?

@timdorr
Copy link
Member

timdorr commented Jun 22, 2021

Also, the toggle does cause the page layout to change and the scroll position to move. Any way to avoid that? Not a dealbreaker, just an annoyance.

@markerikson
Copy link
Contributor Author

No, the TS compiler is set up to effectively just strip the types.

For that example, the TS source is:

import { createSlice, PayloadAction } from '@reduxjs/toolkit'

interface CounterState {
  value: number
}

const initialState = { value: 0 } as CounterState

const counterSlice = createSlice({
  name: 'counter',
  initialState,
  reducers: {
    increment(state) {
      state.value++
    },
    decrement(state) {
      state.value--
    },
    incrementByAmount(state, action: PayloadAction<number>) {
      state.value += action.payload
    },
  },
})

export const { increment, decrement, incrementByAmount } = counterSlice.actions
export default counterSlice.reducer

and the JS is:

import { createSlice } from '@reduxjs/toolkit'

const initialState = { value: 0 }

const counterSlice = createSlice({
  name: 'counter',
  initialState,
  reducers: {
    increment(state) {
      state.value++
    },
    decrement(state) {
      state.value--
    },
    incrementByAmount(state, action) {
      state.value += action.payload
    },
  },
})

export const { increment, decrement, incrementByAmount } = counterSlice.actions
export default counterSlice.reducer

@markerikson
Copy link
Contributor Author

the only page layout change I'm seeing is that the code block shrinks when you go TS->JS, because there's just fewer lines of code to show. Anything else beyond that?

@timdorr
Copy link
Member

timdorr commented Jun 22, 2021

Yeah, it's just that. If it's avoidable, that would be cool, but I know it's difficult.

@ryota-murakami
Copy link
Contributor

I think JS/TS tab switching UI intended to be one of the solution of resolve less readability by JS/TS code amount gap.
But Lenz prefer show example JS/TS simultaneously like https://shikijs.github.io/twoslash/

To start this, we have to be clear which way goal is.

@YuPototo
Copy link

Hi, can we add TS version for testing docs?

writing tests

@Shrugsy
Copy link
Contributor

Shrugsy commented Sep 26, 2021

Also, the toggle does cause the page layout to change and the scroll position to move. Any way to avoid that? Not a dealbreaker, just an annoyance.

@timdorr @markerikson FYI if facebook/docusaurus#5618 gets accepted, it should address the above page layout shifting

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

No branches or pull requests

5 participants