-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
[WIP] feat(docs):Addition of JS/TS Toggle button in the docs to switch to appropriate code. #3082
Conversation
@antfu I have create a toggle button on the top of the sidebar in this PR. The PR is still in progress as now I have to write all the switchable content for all the functions. I wanted some clarification from you on some thing. In the PR, I have written the typescript code snippet for // store.ts
import { ref } from 'vue'
import { createGlobalState } from '@vueuse/core'
export const useGlobalState = createGlobalState(
() => {
const count = ref(0)
return { count }
}
) This is same as the javascript code. This is because all the types are already inferred from the // store.ts
import { ref } from 'vue'
import { createGlobalState } from '@vueuse/core'
function storageFn() {
const count = ref(0)
return { count }
}
export const useGlobalState = createGlobalState<typeof storageFn>(storageFn) Also request you to take a look at the toggle component I made for toggling the code i.e. |
Thanks! I think we should not manually write js/ts docs version at all, to avoid increasing the maintenance burden. Instead, as I suggested (#2475 (comment)), we should always write in TS, and use a custom plugin to generate the js version from the ts blocks using esbuild at compile time |
Thanks for your reply @antfu . Can you also tell me which of the above typescript code snippets should I use for |
Hey @antfu , As suggested by you, I have created a plugin that will create a JS frame for the typescript snippet it finds in all the |
@antfu Request you to please check if the plugin I created for typescript transpilation to javascript is what we require? I can then move ahead with converting code snippets to typescript for other functions... |
@@ -11,19 +11,24 @@ Keep states in the global scope to be reusable across Vue instances. | |||
|
|||
### Without Persistence (Store in Memory) | |||
|
|||
```js | |||
// store.js | |||
<div class="ts-api"> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we automatically apply the transformation to any ts blocks, without the need of this marker? (so the documents keep untouched)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the <div class="ts-api">...</div>
and <div class="js-api">...</div>
are identifier classes for when to hide which code block when the toggle is used.
Please refer the below code in TsJsToggleSwitch.vue
:
What TsJsToggleSwitch.vue
does is, the default code enabled is the Typescript code, when we toggle to Javascript, the component adds a prefer-js
class to the body. Once this class is attached to the body, the typescript code (one with class ts-api
) gets a display: none;
css property and the Javascript code (one with class js-api
) gets a display: initial;
css property. Similarly, when you toggle it back to typescript, the Javascript code gets removed from the DOM and the Typescript code gets added back to the DOM. Hence the div
elements in the documents.
I have also written a vite plugin tsToJsTransform.ts
, which recognizes the typescript code using the divs with the ts-api
and transpiles the code inside it to Javascript code and adds both the snippets in the file code at build time. Correct me if I am wrong, but we are supposed to rewrite all the JS snippets as TS snippets and transpile those snippets on build time to generate an additional JS code block.
If you have a better approach, please let me know, and I shall try to go that way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@antfu Please let me know if you have a better approach...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@antfu Can you please update on my comment?
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Before submitting the PR, please make sure you do the following
fixes #123
).Description
Resolves #2475
Additional context
This PR introduces a toggle for JS/TS switch on every function page in the vueuse documentation.
This PR is still work in progress, I will mark it for review as soon as it is ready for review.
🤖 Generated by Copilot at 425a218
This pull request adds a feature to the documentation site that allows users to switch between TypeScript and JavaScript code examples. It introduces a new dependency
@vue/theme
and a new componentTsJsToggleSwitch.vue
for the sidebar toggle. It also updates the documentation page forcreateGlobalState
and thepnpm-lock.yaml
file.🤖 Generated by Copilot at 425a218
@vue/theme
dependency to enable a custom theme for the Vitepress documentation site (link, link, link)TsJsToggleSwitch
component to render a language preference switch in the sidebar (link, link, link, link, link)<div class="js-api">
and<div class="ts-api">
tags to the documentation pages to toggle the visibility of JavaScript and TypeScript code examples (link, link)dev: true
property from several dependencies in the lock file that are also used for production (link, link, link, link, link, link, link, link, link, link, link, link, link, link, link, link, link, link, link, link, link, link, link, link, link)body-scroll-lock
dependency to version4.0.0-beta.0
and add version3.1.5
as a dependency of the@vue/theme
package (link)normalize.css
dependency to the@vue/theme
package and remove it from thedev: true
dependencies (link, link)createGlobalState
documentation page for readability (link)