redux icon indicating copy to clipboard operation
redux copied to clipboard

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

Open markerikson opened this issue 3 years ago • 8 comments

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.

markerikson avatar Jun 22 '21 03:06 markerikson

Does the JS come out looking weird at all?

timdorr avatar Jun 22 '21 03:06 timdorr

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 avatar Jun 22 '21 03:06 timdorr

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 avatar Jun 22 '21 03:06 markerikson

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?

markerikson avatar Jun 22 '21 03:06 markerikson

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

timdorr avatar Jun 22 '21 03:06 timdorr

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.

ryota-murakami avatar Jun 22 '21 04:06 ryota-murakami

Hi, can we add TS version for testing docs?

writing tests

YuPototo avatar Jul 30 '21 09:07 YuPototo

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 https://github.com/facebook/docusaurus/pull/5618 gets accepted, it should address the above page layout shifting

Shrugsy avatar Sep 26 '21 04:09 Shrugsy